- int s;
-
- if (!(proto = getprotobyname("tcp")))
- return 0;
- if ((s = socket(AF_INET, SOCK_STREAM, proto->p_proto)) < 0)
- return 0;
- if (!(p = malloc(sizeof(struct comstack))))
- return 0;
- if (!(state = p->private = malloc(sizeof(tcpip_state))))
- return 0;
+ int new_socket;
+#ifdef WINDOWS
+ unsigned long tru = 1;
+#else
+ struct protoent *proto;
+#endif
+
+ if (!initialized)
+ {
+#ifdef WINDOWS
+ WORD requested;
+ WSADATA wd;
+
+ requested = MAKEWORD(1, 1);
+ if (WSAStartup(requested, &wd))
+ return 0;
+#endif
+ initialized = 1;
+ }
+
+ if (s < 0)
+ {
+#ifndef WINDOWS
+ if (!(proto = getprotobyname("tcp")))
+ return 0;
+ if ((s = socket(AF_INET, SOCK_STREAM, proto->p_proto)) < 0)
+#else
+ if ((s = socket(AF_INET, SOCK_STREAM, 0)) < 0)
+#endif
+ return 0;
+ new_socket = 1;
+ }
+ else
+ new_socket = 0;
+ if (!(p = xmalloc(sizeof(struct comstack))))
+ return 0;
+ if (!(state = p->private = xmalloc(sizeof(tcpip_state))))
+ return 0;
+
+#ifdef WINDOWS
+ if (!(p->blocking = blocking) && ioctlsocket(s, FIONBIO, &tru) < 0)
+#else