// File: /workspace/github_cpp_makefile_star2_O1/NovaGL[P]HarmonyHubControl/csocket.cpp
// Function: csocket::connect(char const*, unsigned int)
int csocket::connect( const char* remoteHost, unsigned int remotePort )
{
    if ( m_socketState != CLOSED ) 
    {
        return ERROR_INAPPROPRIATE_STATE;
    }

    struct hostent *pHostEnt = NULL;
    int status = resolveHost(remoteHost, &pHostEnt);
    if (status == FAILURE || !pHostEnt) 
    {
        return FAILURE;
    }

    m_strRemoteHost = remoteHost;
    m_remotePort = remotePort;

    m_remoteSocketAddr.sin_family = pHostEnt->h_addrtype;
    memcpy((char *) &m_remoteSocketAddr.sin_addr.s_addr,
        pHostEnt->h_addr_list[0], 
        pHostEnt->h_length);

    m_remoteSocketAddr.sin_port = htons(m_remotePort);


#ifdef _WIN32
    m_socket = WSASocket(AF_INET, SOCK_STREAM, 
        IPPROTO_TCP, 0 , 0 , 0);

    if (m_socket == INVALID_SOCKET)
    {
        return FAILURE;
    }
#else
    m_socket = socket(AF_INET, SOCK_STREAM, 0);
#endif

    if (m_socket < 0) 
    {
        return FAILURE;
    }

    m_localSocketAddr.sin_family = AF_INET;

    m_localSocketAddr.sin_addr.s_addr = htonl(INADDR_ANY);
    
    int         iRecvTimeout = 1;
    setsockopt(m_socket, SOL_SOCKET, SO_RCVTIMEO, 
        (char*) &iRecvTimeout, sizeof(iRecvTimeout) );

    m_localSocketAddr.sin_port = htons(0);

    status = bind( m_socket, 
        (const sockaddr*)&(m_localSocketAddr),  
        sizeof(struct sockaddr) );

    if (status < 0) 
    {
        return FAILURE;
    }

#ifdef _WIN32 
    int set = 1;
    setsockopt(m_socket, IPPROTO_TCP, TCP_NODELAY,  (char*) &set, sizeof(set) );
#endif

    m_remoteSocketAddr.sin_family = pHostEnt->h_addrtype;

    memcpy(&(m_remoteSocketAddr.sin_addr), 
                pHostEnt->h_addr, 
                pHostEnt->h_length);

    m_remoteSocketAddr.sin_port = htons( m_remotePort );

    status = ::connect(m_socket, 
                    (const sockaddr*)&(m_remoteSocketAddr), 
                    sizeof(m_remoteSocketAddr)  );

    if (status < 0) 
    {
        return FAILURE;
    }

    m_socketState = CONNECTED;

    return SUCCESS;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x38, %rsp
movl $0xffffffff, %ebp       # imm = 0xFFFFFFFF
cmpl $0x0, 0x2c(%rdi)
jne 0xbca9
movl %edx, %r14d
movq %rsi, %r15
movq %rdi, %rbx
leaq 0x10(%rsp), %r12
movq $0x0, (%r12)
leaq 0x18(%rsp), %r13
leaq 0xf(%rsp), %rdx
movq %r13, %rdi
callq 0x7de4
movq %r13, %rdi
movq %r12, %rsi
callq 0xbaae
movl %eax, %ebp
leaq 0x28(%rsp), %rax
movq -0x10(%rax), %rdi
cmpq %rax, %rdi
je 0xbb7d
movq 0x28(%rsp), %rsi
incq %rsi
callq 0x3290
testl %ebp, %ebp
sete %al
movq 0x10(%rsp), %r13
testq %r13, %r13
setne %cl
movl $0x1, %ebp
testb %cl, %al
je 0xbca9
leaq 0x30(%rbx), %r12
movq 0x38(%rbx), %rbp
movq %r15, %rdi
callq 0x3150
movq %r12, %rdi
xorl %esi, %esi
movq %rbp, %rdx
movq %r15, %rcx
movq %rax, %r8
callq 0x3420
movl %r14d, 0x50(%rbx)
movzwl 0x10(%r13), %eax
movw %ax, 0x1c(%rbx)
leaq 0x20(%rbx), %r14
movq 0x18(%r13), %rax
movq (%rax), %rsi
movslq 0x14(%r13), %rdx
movq %r14, %rdi
callq 0x31f0
movzwl 0x50(%rbx), %eax
rolw $0x8, %ax
movw %ax, 0x1e(%rbx)
movl $0x1, %ebp
movl $0x2, %edi
movl $0x1, %esi
xorl %edx, %edx
callq 0x30c0
movl %eax, 0x8(%rbx)
testl %eax, %eax
js 0xbca9
leaq 0xc(%rbx), %r15
movw $0x2, 0xc(%rbx)
movl $0x0, 0x10(%rbx)
leaq 0x18(%rsp), %rcx
movl $0x1, (%rcx)
movl $0x1, %ebp
movl %eax, %edi
movl $0x1, %esi
movl $0x14, %edx
movl $0x4, %r8d
callq 0x34b0
movw $0x0, 0xe(%rbx)
movl 0x8(%rbx), %edi
movq %r15, %rsi
movl $0x10, %edx
callq 0x3070
testl %eax, %eax
js 0xbca9
leaq 0x1c(%rbx), %r15
movzwl 0x10(%r13), %eax
movw %ax, 0x1c(%rbx)
movq 0x18(%r13), %rax
movq (%rax), %rsi
movslq 0x14(%r13), %rdx
movq %r14, %rdi
callq 0x31f0
movzwl 0x50(%rbx), %eax
rolw $0x8, %ax
movw %ax, 0x1e(%rbx)
movl 0x8(%rbx), %edi
movq %r15, %rsi
movl $0x10, %edx
callq 0x3200
testl %eax, %eax
js 0xbca9
movl $0x1, 0x2c(%rbx)
xorl %ebp, %ebp
movl %ebp, %eax
addq $0x38, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movq %rax, %rbx
leaq 0x28(%rsp), %rax
movq -0x10(%rax), %rdi
cmpq %rax, %rdi
je 0xbcd8
movq 0x28(%rsp), %rsi
incq %rsi
callq 0x3290
movq %rbx, %rdi
callq 0x34a0