+ /*
+ * We expect a potential EWOULDBLOCK here, because the
+ * chances are the front end has done a select for
+ * FD_CONNECT, so that connect() will complete
+ * asynchronously.
+ */
+ if ( err != WSAEWOULDBLOCK ) {
+ ret->error = winsock_error_string(err);
+ return (Socket) ret;
+ }
+ } else {
+ /*
+ * If we _don't_ get EWOULDBLOCK, the connect has completed
+ * and we should set the socket as writable.
+ */
+ ret->writable = 1;
+ }
+
+ add234(sktree, ret);
+
+ return (Socket) ret;
+}
+
+Socket sk_newlistener(int port, Plug plug, int local_host_only)
+{
+ static struct socket_function_table fn_table = {
+ sk_tcp_plug,
+ sk_tcp_close,
+ sk_tcp_write,
+ sk_tcp_write_oob,
+ sk_tcp_flush,
+ sk_tcp_socket_error
+ };
+
+ SOCKET s;
+#ifdef IPV6
+ SOCKADDR_IN6 a6;
+#endif
+ SOCKADDR_IN a;
+ DWORD err;
+ char *errstr;
+ Actual_Socket ret;
+ int retcode;
+ int on = 1;
+
+ /*
+ * Create Socket structure.
+ */
+ ret = smalloc(sizeof(struct Socket_tag));
+ ret->fn = &fn_table;
+ ret->error = NULL;
+ ret->plug = plug;
+ bufchain_init(&ret->output_data);
+ ret->writable = 0; /* to start with */
+ ret->sending_oob = 0;
+ ret->frozen = 0;
+ ret->frozen_readable = 0;
+ ret->localhost_only = local_host_only;
+ ret->pending_error = 0;
+
+ /*
+ * Open socket.
+ */
+ s = socket(AF_INET, SOCK_STREAM, 0);
+ ret->s = s;
+
+ if (s == INVALID_SOCKET) {
+ err = WSAGetLastError();
+ ret->error = winsock_error_string(err);
+ return (Socket) ret;
+ }
+
+ ret->oobinline = 0;
+
+
+ setsockopt(s, SOL_SOCKET, SO_REUSEADDR, (const char *)&on, sizeof(on));
+
+
+#ifdef IPV6
+ if (addr->family == AF_INET6) {
+ memset(&a6, 0, sizeof(a6));
+ a6.sin6_family = AF_INET6;
+ if (local_host_only)
+ a6.sin6_addr = in6addr_loopback;
+ else
+ a6.sin6_addr = in6addr_any;
+ a6.sin6_port = htons(port);
+ } else
+#endif
+ {
+ a.sin_family = AF_INET;
+ if (local_host_only)
+ a.sin_addr.s_addr = htonl(INADDR_LOOPBACK);
+ else
+ a.sin_addr.s_addr = htonl(INADDR_ANY);
+ a.sin_port = htons((short)port);
+ }
+#ifdef IPV6
+ retcode = bind(s, (addr->family == AF_INET6 ?
+ (struct sockaddr *) &a6 :
+ (struct sockaddr *) &a),
+ (addr->family ==
+ AF_INET6 ? sizeof(a6) : sizeof(a)));
+#else
+ retcode = bind(s, (struct sockaddr *) &a, sizeof(a));
+#endif
+ if (retcode != SOCKET_ERROR) {
+ err = 0;
+ } else {
+ err = WSAGetLastError();
+ }
+
+ if (err) {
+ ret->error = winsock_error_string(err);
+ return (Socket) ret;
+ }
+
+
+ if (listen(s, SOMAXCONN) == SOCKET_ERROR) {
+ closesocket(s);
+ ret->error = winsock_error_string(err);
+ return (Socket) ret;