diff --git a/lib/libwebsockets.c b/lib/libwebsockets.c
index 5ab49a1293655909409187bd1fe75182c3e6d455..dc4aa5f4c231ace2dcbed7c1213b376666cb8fa2 100755
--- a/lib/libwebsockets.c
+++ b/lib/libwebsockets.c
@@ -408,7 +408,9 @@ just_kill_connection:
 		char* errorString;
 
 		lwsl_debug("Connection closed before server reply\n");
-		errorString = lws_hdr_simple_ptr(wsi, WSI_TOKEN_HTTP);
+		errorString = NULL;
+		if (wsi->u.hdr.ah)
+			errorString = lws_hdr_simple_ptr(wsi, WSI_TOKEN_HTTP);
 		if (errorString) {
 			context->protocols[0].callback(wsi,
 					LWS_CALLBACK_CLIENT_CONNECTION_ERROR,