Browse code

Fix crash on Windows when --enable-stats is set

Shawn Webb authored on 2014/05/07 03:44:24
Showing 1 changed files
... ...
@@ -56,6 +56,15 @@ int connect_host(const char *host, const char *port, uint32_t timeout, int useAs
56 56
     socklen_t len;
57 57
     fd_set read_fds, write_fds;
58 58
     struct timeval tv;
59
+#ifdef _WIN32
60
+	int iResult;
61
+	WSADATA wsaData;
62
+
63
+	/* Force initialization of Windows sockets, even if it already happened elsewhere */
64
+	iResult = WSAStartup(MAKEWORD(2,2), &wsaData);
65
+	if (iResult != 0)
66
+		return -1;
67
+#endif
59 68
 
60 69
     memset(&hints, 0x00, sizeof(struct addrinfo));
61 70
     hints.ai_family = AF_UNSPEC;
... ...
@@ -80,7 +89,9 @@ int connect_host(const char *host, const char *port, uint32_t timeout, int useAs
80 80
         if ((error = connect(sockfd, p->ai_addr, p->ai_addrlen))) {
81 81
             if (useAsync) {
82 82
                 if (errno != EINPROGRESS) {
83
+#ifndef _WIN32
83 84
                     close(sockfd);
85
+#endif
84 86
                     continue;
85 87
                 }
86 88
                 errno = 0;
... ...
@@ -121,8 +132,10 @@ int connect_host(const char *host, const char *port, uint32_t timeout, int useAs
121 121
 
122 122
     if (!(p)) {
123 123
         freeaddrinfo(servinfo);
124
+#ifndef _WIN32
124 125
         if (sockfd >= 0)
125 126
             close(sockfd);
127
+#endif
126 128
         return -1;
127 129
     }
128 130