crm_remote_tcp_connect(const char *host, int port)
{
    struct addrinfo *res;
    struct addrinfo *rp;
    struct addrinfo hints;
    const char *server = host;
    int ret_ga;
    int sock;

    /* getaddrinfo */
    memset(&hints, 0, sizeof(struct addrinfo));
    hints.ai_family = AF_UNSPEC;    /* Allow IPv4 or IPv6 */
    hints.ai_socktype = SOCK_STREAM;
    hints.ai_flags = AI_CANONNAME;

    crm_debug("Looking up %s", server);
    ret_ga = getaddrinfo(server, NULL, &hints, &res);
    if (ret_ga) {
        crm_err("getaddrinfo: %s", gai_strerror(ret_ga));
        return -1;
    }

    if (!res || !res->ai_addr) {
        crm_err("getaddrinfo failed");
        return -1;
    }

    for (rp = res; rp != NULL; rp = rp->ai_next) {
        struct sockaddr *addr = rp->ai_addr;
        int flag = 0;
        if (!addr) {
            continue;
        }

        if (rp->ai_canonname) {
            server = res->ai_canonname;
        }
        crm_debug("Got address %s for %s", server, host);

        /* create socket */
        sock = socket(rp->ai_family, SOCK_STREAM, IPPROTO_TCP);
        if (sock == -1) {
            crm_err("Socket creation failed for remote client connection.");
            continue;
        }
        if (addr->sa_family == AF_INET6) {
            struct sockaddr_in6 *addr_in = (struct sockaddr_in6 *) addr;
            addr_in->sin6_port = htons(port);
        } else {
            struct sockaddr_in *addr_in = (struct sockaddr_in *) addr;
            addr_in->sin_port = htons(port);
            crm_info("Attempting to connect to remote server at %s:%d", inet_ntoa(addr_in->sin_addr), port);
        }

        if (connect(sock, rp->ai_addr, rp->ai_addrlen) == 0) {
            if ((flag = fcntl(sock, F_GETFL)) >= 0) {
                if (fcntl(sock, F_SETFL, flag | O_NONBLOCK) < 0) {
                    crm_err( "fcntl() write failed");
                    close(sock);
                    sock = -1;
                    continue;
                }
            }
            break;                  /* Success */
        }

        close(sock);
        sock = -1;
    }
    freeaddrinfo(res);

    return sock;
}
crm_remote_tcp_connect(const char *host, int port)
{
    struct addrinfo *res;
    struct addrinfo *rp;
    struct addrinfo hints;
    const char *server = host;
    int ret_ga;
    int sock;

     
    memset(&hints, 0, sizeof(struct addrinfo));
    hints.ai_family = AF_UNSPEC;     
    hints.ai_socktype = SOCK_STREAM;
    hints.ai_flags = AI_CANONNAME;

    crm_debug("Looking up %s", server);
    ret_ga = getaddrinfo(server, NULL, &hints, &res);
    if (ret_ga) {
        crm_err("getaddrinfo: %s", gai_strerror(ret_ga));
        return -1;
    }

    if (!res || !res->ai_addr) {
        crm_err("getaddrinfo failed");
        return -1;
    }

    for (rp = res; rp != NULL; rp = rp->ai_next) {
        struct sockaddr *addr = rp->ai_addr;
        int flag = 0;
        if (!addr) {
            continue;
        }

        if (rp->ai_canonname) {
            server = res->ai_canonname;
        }
        crm_debug("Got address %s for %s", server, host);

         
        sock = socket(rp->ai_family, SOCK_STREAM, IPPROTO_TCP);
        if (sock == -1) {
            crm_err("Socket creation failed for remote client connection.");
            continue;
        }
        if (addr->sa_family == AF_INET6) {
            struct sockaddr_in6 *addr_in = (struct sockaddr_in6 *) addr;
            addr_in->sin6_port = htons(port);
        } else {
            struct sockaddr_in *addr_in = (struct sockaddr_in *) addr;
            addr_in->sin_port = htons(port);
            crm_info("Attempting to connect to remote server at %s:%d", inet_ntoa(addr_in->sin_addr), port);
        }

        if (connect(sock, rp->ai_addr, rp->ai_addrlen) == 0) {
            if ((flag = fcntl(sock, F_GETFL)) >= 0) {
                if (fcntl(sock, F_SETFL, flag | O_NONBLOCK) < 0) {
                    crm_err( "fcntl() write failed");
                    close(sock);
                    sock = -1;
                    continue;
                }
            }
            break;                   
        }

        close(sock);
        sock = -1;
    }
    freeaddrinfo(res);

    return sock;
}