defer changing to proxy uri until connected

Reported-by: shyswork
Signed-off-by: Andy Green <andy.green@linaro.org>
diff --git a/lib/client-handshake.c b/lib/client-handshake.c
index 24968e7..a241c90 100644
--- a/lib/client-handshake.c
+++ b/lib/client-handshake.c
@@ -25,25 +25,16 @@
 			"\x0d\x0a",
 			lws_hdr_simple_ptr(wsi, _WSI_TOKEN_CLIENT_PEER_ADDRESS),
 			wsi->u.hdr.ah->c_port);
-
-		/* OK from now on we talk via the proxy, so connect to that */
-
-		/*
-		 * (will overwrite existing pointer,
-		 * leaving old string/frag there but unreferenced)
-		 */
-		if (lws_hdr_simple_create(wsi, _WSI_TOKEN_CLIENT_PEER_ADDRESS,
-						   context->http_proxy_address))
-			goto oom4;
-		wsi->u.hdr.ah->c_port = context->http_proxy_port;
+		ads = context->http_proxy_address;
+		server_addr.sin_port = htons(context->http_proxy_port);
+	} else {
+		ads = lws_hdr_simple_ptr(wsi, _WSI_TOKEN_CLIENT_PEER_ADDRESS);
+		server_addr.sin_port = htons(wsi->u.hdr.ah->c_port);
 	}
 
 	/*
 	 * prepare the actual connection (to the proxy, if any)
 	 */
-
-	ads = lws_hdr_simple_ptr(wsi, _WSI_TOKEN_CLIENT_PEER_ADDRESS);
-
 	lwsl_client("__libwebsocket_client_connect_2: address %s\n", ads);
 
 	server_hostent = gethostbyname(ads);
@@ -77,8 +68,8 @@
 	}
 
 	server_addr.sin_family = AF_INET;
-	server_addr.sin_port = htons(wsi->u.hdr.ah->c_port);
 	server_addr.sin_addr = *((struct in_addr *)server_hostent->h_addr);
+
 	bzero(&server_addr.sin_zero, 8);
 
 	if (connect(wsi->sock, (struct sockaddr *)&server_addr,
@@ -112,6 +103,17 @@
 
 	if (context->http_proxy_port) {
 
+		/* OK from now on we talk via the proxy, so connect to that */
+
+		/*
+		 * (will overwrite existing pointer,
+		 * leaving old string/frag there but unreferenced)
+		 */
+		if (lws_hdr_simple_create(wsi, _WSI_TOKEN_CLIENT_PEER_ADDRESS,
+						   context->http_proxy_address))
+			goto failed;
+		wsi->u.hdr.ah->c_port = context->http_proxy_port;
+
 		n = send(wsi->sock, context->service_buffer, plen, MSG_NOSIGNAL);
 		if (n < 0) {
 			lwsl_debug("ERROR writing to proxy socket\n");