diff --git a/lib/connect.c b/lib/connect.c
index 5a376c34aaebd2eb6ad33e5f5c819eefd50ee489..90283fe85cca6dbb64bf383d6e2e7f4ae40018c6 100644
--- a/lib/connect.c
+++ b/lib/connect.c
@@ -570,11 +570,11 @@ static CURLcode trynextip(struct connectdata *conn,
 
       if(ai) {
         rc = singleipconnect(conn, ai, &conn->tempsock[tempindex]);
-        conn->tempaddr[tempindex] = ai;
         if(rc == CURLE_COULDNT_CONNECT) {
           ai = ai->ai_next;
           continue;
         }
+        conn->tempaddr[tempindex] = ai;
       }
       break;
     }