From 0bbcdd38545b2877b9b034fd4e96cb9760d79ea5 Mon Sep 17 00:00:00 2001
From: Fabian Keil <fk@fabiankeil.de>
Date: Sat, 16 Nov 2013 19:02:32 +0100
Subject: [PATCH] lib/connect.c: Close open but but unconnected sockets in
 Curl_connecthost()

This is necessary if singleipconnect() fails with CURLE_COULDNT_CONNECT
for the last address to try, in which case the open but unconnected socket
will not be dealt with by trynextip().

On FreeBSD, this commit makes tests 19 and 704 reliable again after 02fbc26d5.
---
 lib/connect.c | 7 +++++++
 1 file changed, 7 insertions(+)

diff --git a/lib/connect.c b/lib/connect.c
index e4d4fee..e99e873 100644
--- a/lib/connect.c
+++ b/lib/connect.c
@@ -1099,6 +1099,13 @@ CURLcode Curl_connecthost(struct connectdata *conn,  /* context */
         conn->tempsock[0] == CURL_SOCKET_BAD)
     res = trynextip(conn, FIRSTSOCKET, 0);
 
+  if(res == CURLE_COULDNT_CONNECT &&
+     conn->tempsock[0] != CURL_SOCKET_BAD) {
+    /* We ended up with an open but unconnected socket.
+       Close it, as it isn't useful to the caller. */
+    Curl_closesocket(conn, conn->tempsock[0]);
+    conn->tempsock[0] = CURL_SOCKET_BAD;
+  }
   if(conn->tempsock[0] == CURL_SOCKET_BAD)
     return res;
 
-- 
1.8.4


