static int conn_state(BIO *b, BIO_CONNECT *c)
{
    int ret = -1, i;
    BIO_info_cb *cb = NULL;
    if (c->info_callback != NULL)
        cb = c->info_callback;
    for (;;) {
        switch (c->state) {
        case BIO_CONN_S_BEFORE:
            if (c->param_hostname == NULL && c->param_service == NULL) {
                BIOerr(BIO_F_CONN_STATE, BIO_R_NO_HOSTNAME_OR_SERVICE_SPECIFIED);
                ERR_add_error_data(4,
                                   "hostname=", c->param_hostname,
                                   " service=", c->param_service);
                goto exit_loop;
            }
            c->state = BIO_CONN_S_GET_ADDR;
            break;
        case BIO_CONN_S_GET_ADDR:
            {
                int family = AF_UNSPEC;
                switch (c->connect_family) {
                case BIO_FAMILY_IPV6:
                    if (1) {
#ifdef AF_INET6
                        family = AF_INET6;
                    } else {
#endif
                        BIOerr(BIO_F_CONN_STATE, BIO_R_UNAVAILABLE_IP_FAMILY);
                        goto exit_loop;
                    }
                    break;
                case BIO_FAMILY_IPV4:
                    family = AF_INET;
                    break;
                case BIO_FAMILY_IPANY:
                    family = AF_UNSPEC;
                    break;
                default:
                    BIOerr(BIO_F_CONN_STATE, BIO_R_UNSUPPORTED_IP_FAMILY);
                    goto exit_loop;
                }
                if (BIO_lookup(c->param_hostname, c->param_service,
                               BIO_LOOKUP_CLIENT,
                               family, SOCK_STREAM, &c->addr_first) == 0)
                    goto exit_loop;
            }
            if (c->addr_first == NULL) {
                BIOerr(BIO_F_CONN_STATE, BIO_R_LOOKUP_RETURNED_NOTHING);
                goto exit_loop;
            }
            c->addr_iter = c->addr_first;
            c->state = BIO_CONN_S_CREATE_SOCKET;
            break;
        case BIO_CONN_S_CREATE_SOCKET:
            ret = BIO_socket(BIO_ADDRINFO_family(c->addr_iter),
                             BIO_ADDRINFO_socktype(c->addr_iter),
                             BIO_ADDRINFO_protocol(c->addr_iter), 0);
            if (ret == (int)INVALID_SOCKET) {
                FUNCerr("socket", get_last_socket_error());
                ERR_add_error_data(4,
                                   "hostname=", c->param_hostname,
                                   " service=", c->param_service);
                BIOerr(BIO_F_CONN_STATE, BIO_R_UNABLE_TO_CREATE_SOCKET);
                goto exit_loop;
            }
            b->num = ret;
            c->state = BIO_CONN_S_CONNECT;
            break;
        case BIO_CONN_S_CONNECT:
            BIO_clear_retry_flags(b);
            ret = BIO_connect(b->num, BIO_ADDRINFO_address(c->addr_iter),
                              BIO_SOCK_KEEPALIVE | c->connect_mode);
            b->retry_reason = 0;
            if (ret == 0) {
                if (BIO_sock_should_retry(ret)) {
                    BIO_set_retry_special(b);
                    c->state = BIO_CONN_S_BLOCKED_CONNECT;
                    b->retry_reason = BIO_RR_CONNECT;
                    ERR_clear_error();
                } else if ((c->addr_iter = BIO_ADDRINFO_next(c->addr_iter))
                           != NULL) {
                    BIO_closesocket(b->num);
                    c->state = BIO_CONN_S_CREATE_SOCKET;
                    ERR_clear_error();
                    break;
                } else {
                    FUNCerr("connect", get_last_socket_error());
                    ERR_add_error_data(4,
                                       "hostname=", c->param_hostname,
                                       " service=", c->param_service);
                    BIOerr(BIO_F_CONN_STATE, BIO_R_CONNECT_ERROR);
                }
                goto exit_loop;
            } else {
                c->state = BIO_CONN_S_OK;
            }
            break;
        case BIO_CONN_S_BLOCKED_CONNECT:
            i = BIO_sock_error(b->num);
            if (i) {
                BIO_clear_retry_flags(b);
                FUNCerr("connect", i);
                ERR_add_error_data(4,
                                   "hostname=", c->param_hostname,
                                   " service=", c->param_service);
                BIOerr(BIO_F_CONN_STATE, BIO_R_NBIO_CONNECT_ERROR);
                ret = 0;
                goto exit_loop;
            } else
                c->state = BIO_CONN_S_OK;
            break;
        case BIO_CONN_S_OK:
            ret = 1;
            goto exit_loop;
        default:
            goto exit_loop;
        }
        if (cb != NULL) {
            if ((ret = cb((BIO *)b, c->state, ret)) == 0)
                goto end;
        }
    }
 exit_loop:
    if (cb != NULL)
        ret = cb((BIO *)b, c->state, ret);
 end:
    return ret;
}
['static int conn_state(BIO *b, BIO_CONNECT *c)\n{\n    int ret = -1, i;\n    BIO_info_cb *cb = NULL;\n    if (c->info_callback != NULL)\n        cb = c->info_callback;\n    for (;;) {\n        switch (c->state) {\n        case BIO_CONN_S_BEFORE:\n            if (c->param_hostname == NULL && c->param_service == NULL) {\n                BIOerr(BIO_F_CONN_STATE, BIO_R_NO_HOSTNAME_OR_SERVICE_SPECIFIED);\n                ERR_add_error_data(4,\n                                   "hostname=", c->param_hostname,\n                                   " service=", c->param_service);\n                goto exit_loop;\n            }\n            c->state = BIO_CONN_S_GET_ADDR;\n            break;\n        case BIO_CONN_S_GET_ADDR:\n            {\n                int family = AF_UNSPEC;\n                switch (c->connect_family) {\n                case BIO_FAMILY_IPV6:\n                    if (1) {\n#ifdef AF_INET6\n                        family = AF_INET6;\n                    } else {\n#endif\n                        BIOerr(BIO_F_CONN_STATE, BIO_R_UNAVAILABLE_IP_FAMILY);\n                        goto exit_loop;\n                    }\n                    break;\n                case BIO_FAMILY_IPV4:\n                    family = AF_INET;\n                    break;\n                case BIO_FAMILY_IPANY:\n                    family = AF_UNSPEC;\n                    break;\n                default:\n                    BIOerr(BIO_F_CONN_STATE, BIO_R_UNSUPPORTED_IP_FAMILY);\n                    goto exit_loop;\n                }\n                if (BIO_lookup(c->param_hostname, c->param_service,\n                               BIO_LOOKUP_CLIENT,\n                               family, SOCK_STREAM, &c->addr_first) == 0)\n                    goto exit_loop;\n            }\n            if (c->addr_first == NULL) {\n                BIOerr(BIO_F_CONN_STATE, BIO_R_LOOKUP_RETURNED_NOTHING);\n                goto exit_loop;\n            }\n            c->addr_iter = c->addr_first;\n            c->state = BIO_CONN_S_CREATE_SOCKET;\n            break;\n        case BIO_CONN_S_CREATE_SOCKET:\n            ret = BIO_socket(BIO_ADDRINFO_family(c->addr_iter),\n                             BIO_ADDRINFO_socktype(c->addr_iter),\n                             BIO_ADDRINFO_protocol(c->addr_iter), 0);\n            if (ret == (int)INVALID_SOCKET) {\n                FUNCerr("socket", get_last_socket_error());\n                ERR_add_error_data(4,\n                                   "hostname=", c->param_hostname,\n                                   " service=", c->param_service);\n                BIOerr(BIO_F_CONN_STATE, BIO_R_UNABLE_TO_CREATE_SOCKET);\n                goto exit_loop;\n            }\n            b->num = ret;\n            c->state = BIO_CONN_S_CONNECT;\n            break;\n        case BIO_CONN_S_CONNECT:\n            BIO_clear_retry_flags(b);\n            ret = BIO_connect(b->num, BIO_ADDRINFO_address(c->addr_iter),\n                              BIO_SOCK_KEEPALIVE | c->connect_mode);\n            b->retry_reason = 0;\n            if (ret == 0) {\n                if (BIO_sock_should_retry(ret)) {\n                    BIO_set_retry_special(b);\n                    c->state = BIO_CONN_S_BLOCKED_CONNECT;\n                    b->retry_reason = BIO_RR_CONNECT;\n                    ERR_clear_error();\n                } else if ((c->addr_iter = BIO_ADDRINFO_next(c->addr_iter))\n                           != NULL) {\n                    BIO_closesocket(b->num);\n                    c->state = BIO_CONN_S_CREATE_SOCKET;\n                    ERR_clear_error();\n                    break;\n                } else {\n                    FUNCerr("connect", get_last_socket_error());\n                    ERR_add_error_data(4,\n                                       "hostname=", c->param_hostname,\n                                       " service=", c->param_service);\n                    BIOerr(BIO_F_CONN_STATE, BIO_R_CONNECT_ERROR);\n                }\n                goto exit_loop;\n            } else {\n                c->state = BIO_CONN_S_OK;\n            }\n            break;\n        case BIO_CONN_S_BLOCKED_CONNECT:\n            i = BIO_sock_error(b->num);\n            if (i) {\n                BIO_clear_retry_flags(b);\n                FUNCerr("connect", i);\n                ERR_add_error_data(4,\n                                   "hostname=", c->param_hostname,\n                                   " service=", c->param_service);\n                BIOerr(BIO_F_CONN_STATE, BIO_R_NBIO_CONNECT_ERROR);\n                ret = 0;\n                goto exit_loop;\n            } else\n                c->state = BIO_CONN_S_OK;\n            break;\n        case BIO_CONN_S_OK:\n            ret = 1;\n            goto exit_loop;\n        default:\n            goto exit_loop;\n        }\n        if (cb != NULL) {\n            if ((ret = cb((BIO *)b, c->state, ret)) == 0)\n                goto end;\n        }\n    }\n exit_loop:\n    if (cb != NULL)\n        ret = cb((BIO *)b, c->state, ret);\n end:\n    return ret;\n}', 'int BIO_ADDRINFO_family(const BIO_ADDRINFO *bai)\n{\n    if (bai != NULL)\n        return bai->bai_family;\n    return 0;\n}', 'int BIO_ADDRINFO_socktype(const BIO_ADDRINFO *bai)\n{\n    if (bai != NULL)\n        return bai->bai_socktype;\n    return 0;\n}', 'int BIO_ADDRINFO_protocol(const BIO_ADDRINFO *bai)\n{\n    if (bai != NULL) {\n        if (bai->bai_protocol != 0)\n            return bai->bai_protocol;\n#ifdef AF_UNIX\n        if (bai->bai_family == AF_UNIX)\n            return 0;\n#endif\n        switch (bai->bai_socktype) {\n        case SOCK_STREAM:\n            return IPPROTO_TCP;\n        case SOCK_DGRAM:\n            return IPPROTO_UDP;\n        default:\n            break;\n        }\n    }\n    return 0;\n}', 'int BIO_socket(int domain, int socktype, int protocol, int options)\n{\n    int sock = -1;\n    if (BIO_sock_init() != 1)\n        return INVALID_SOCKET;\n    sock = socket(domain, socktype, protocol);\n    if (sock == -1) {\n        FUNCerr("socket", get_last_socket_error());\n        BIOerr(BIO_F_BIO_SOCKET, BIO_R_UNABLE_TO_CREATE_SOCKET);\n        return INVALID_SOCKET;\n    }\n    return sock;\n}', 'int BIO_sock_init(void)\n{\n# ifdef OPENSSL_SYS_WINDOWS\n    static struct WSAData wsa_state;\n    if (!wsa_init_done) {\n        wsa_init_done = 1;\n        memset(&wsa_state, 0, sizeof(wsa_state));\n        if (WSAStartup(0x0202, &wsa_state) != 0) {\n            FUNCerr("wsastartup", get_last_socket_error());\n            BIOerr(BIO_F_BIO_SOCK_INIT, BIO_R_WSASTARTUP);\n            return -1;\n        }\n    }\n# endif\n# ifdef WATT32\n    extern int _watt_do_exit;\n    _watt_do_exit = 0;\n    if (sock_init())\n        return -1;\n# endif\n    return 1;\n}']