diff --git a/lib/connect.c b/lib/connect.c
index 23c192c7db762db77f5900f5bed431c3ac1e50f6..18c40f9acf3d13e1f2b76759c46b05dca96a47a0 100644
--- a/lib/connect.c
+++ b/lib/connect.c
@@ -472,6 +472,9 @@ static bool trynextip(struct connectdata *conn,
   if(sockindex != FIRSTSOCKET)
     return TRUE; /* no next */
 
+  /* first close the failed socket */
+  sclose(conn->sock[sockindex]);
+
   /* try the next address */
   ai = conn->ip_addr->ai_next;