assert(addr->family != AF_UNSPEC);
Socket sk_new(SockAddr addr, int port, int privport, int oobinline,	      int nodelay, int keepalive, Plug plug){    static const struct socket_function_table fn_table = {	sk_tcp_plug,	sk_tcp_close,	sk_tcp_write,	sk_tcp_write_oob,	sk_tcp_flush,	sk_tcp_set_private_ptr,	sk_tcp_get_private_ptr,	sk_tcp_set_frozen,	sk_tcp_socket_error    };    SOCKET s;#ifdef IPV6    SOCKADDR_IN6 a6;#endif    SOCKADDR_IN a;    DWORD err;    char *errstr;    Actual_Socket ret;    short localport;    /*     * Create Socket structure.     */    ret = snew(struct Socket_tag);    ret->fn = &fn_table;    ret->error = NULL;    ret->plug = plug;    bufchain_init(&ret->output_data);    ret->connected = 0;		       /* to start with */    ret->writable = 0;		       /* to start with */    ret->sending_oob = 0;    ret->frozen = 0;    ret->frozen_readable = 0;    ret->localhost_only = 0;	       /* unused, but best init anyway */    ret->pending_error = 0;    /*     * Open socket.     */    assert(addr->family != AF_UNSPEC);    s = p_socket(addr->family, SOCK_STREAM, 0);    ret->s = s;    if (s == INVALID_SOCKET) {	err = p_WSAGetLastError();	ret->error = winsock_error_string(err);	return (Socket) ret;    }    ret->oobinline = oobinline;    if (oobinline) {	BOOL b = TRUE;	p_setsockopt(s, SOL_SOCKET, SO_OOBINLINE, (void *) &b, sizeof(b));    }    if (nodelay) {	BOOL b = TRUE;	p_setsockopt(s, IPPROTO_TCP, TCP_NODELAY, (void *) &b, sizeof(b));    }    if (keepalive) {	BOOL b = TRUE;	p_setsockopt(s, SOL_SOCKET, SO_KEEPALIVE, (void *) &b, sizeof(b));    }    /*     * Bind to local address.     */    if (privport)	localport = 1023;	       /* count from 1023 downwards */    else	localport = 0;		       /* just use port 0 (ie winsock picks) */    /* Loop round trying to bind */    while (1) {	int retcode;#ifdef IPV6	if (addr->family == AF_INET6) {	    memset(&a6, 0, sizeof(a6));	    a6.sin6_family = AF_INET6;/*a6.sin6_addr      = in6addr_any; *//* == 0 */	    a6.sin6_port = p_htons(localport);	} else#endif	{	    a.sin_family = AF_INET;	    a.sin_addr.s_addr = p_htonl(INADDR_ANY);	    a.sin_port = p_htons(localport);	}#ifdef IPV6	retcode = p_bind(s, (addr->family == AF_INET6 ?			   (struct sockaddr *) &a6 :			   (struct sockaddr *) &a),		       (addr->family ==			AF_INET6 ? sizeof(a6) : sizeof(a)));#else	retcode = p_bind(s, (struct sockaddr *) &a, sizeof(a));#endif	if (retcode != SOCKET_ERROR) {	    err = 0;	    break;		       /* done */	} else {	    err = p_WSAGetLastError();	    if (err != WSAEADDRINUSE)  /* failed, for a bad reason */		break;	}	if (localport == 0)	    break;		       /* we're only looping once */	localport--;	if (localport == 0)	    break;		       /* we might have got to the end */    }    if (err) {	ret->error = winsock_error_string(err);	return (Socket) ret;    }    /*     * Connect to remote address.     */#ifdef IPV6    if (addr->family == AF_INET6) {	memset(&a, 0, sizeof(a));	a6.sin6_family = AF_INET6;	a6.sin6_port = p_htons((short) port);	a6.sin6_addr =	    ((struct sockaddr_in6 *) addr->ai->ai_addr)->sin6_addr;    } else#endif    {	a.sin_family = AF_INET;	a.sin_addr.s_addr = p_htonl(addr->address);	a.sin_port = p_htons((short) port);    }    /* Set up a select mechanism. This could be an AsyncSelect on a     * window, or an EventSelect on an event object. */    errstr = do_select(s, 1);    if (errstr) {	ret->error = errstr;	return (Socket) ret;    }    if ((#ifdef IPV6	    p_connect(s, ((addr->family == AF_INET6) ?			(struct sockaddr *) &a6 : (struct sockaddr *) &a),		    (addr->family == AF_INET6) ? sizeof(a6) : sizeof(a))#else	    p_connect(s, (struct sockaddr *) &a, sizeof(a))#endif	) == SOCKET_ERROR) {	err = p_WSAGetLastError();	/*	 * 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);    /* We're done with 'addr' now. */    sk_addr_free(addr);    return (Socket) ret;}