{
ret->oobinline = oobinline; if (oobinline) {
Socket sk_new(SockAddr addr, int port, int privport, sk_receiver_t receiver) {    SOCKET s;#ifdef IPV6    SOCKADDR_IN6 a6;#endif    SOCKADDR_IN a;    DWORD err;    char *errstr;    Socket ret;    extern char *do_select(SOCKET skt, int startup);    short localport;    /*     * Create Socket structure.     */    ret = smalloc(sizeof(struct Socket_tag));    ret->error = NULL;    ret->receiver = receiver;    ret->head = ret->tail = NULL;    ret->writable = 1;		       /* to start with */    ret->sending_oob = 0;    /*     * Open socket.     */    s = socket(addr->family, SOCK_STREAM, 0);    ret->s = s;    if (s == INVALID_SOCKET) {	err = WSAGetLastError();        ret->error = winsock_error_string(err);	return ret;    }    {	BOOL b = TRUE;	setsockopt (s, SOL_SOCKET, SO_OOBINLINE, (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	= htons(localport);        }        else        {#endif            a.sin_family = AF_INET;            a.sin_addr.s_addr = htonl(INADDR_ANY);            a.sin_port = htons(localport);#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;            break;                     /* done */        } else {            err = 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 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 = 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 = htonl(addr->address);	a.sin_port = htons((short)port);#ifdef IPV6    }    if (connect (s, (addr->family == AF_INET6) ? (struct sockaddr *)&a6 : (struct sockaddr *)&a, (addr->family == AF_INET6) ? sizeof(a6) : sizeof(a)) == SOCKET_ERROR)#else	if (connect (s, (struct sockaddr *)&a, sizeof(a)) == SOCKET_ERROR)#endif    {	err = WSAGetLastError();	ret->error = winsock_error_string(err);	return ret;    }    /* 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 ret;    }    add234(sktree, ret);    return ret;}