// File: /workspace/github_cpp_makefile_star2_O2/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 %r12
pushq %rbx
subq $0x30, %rsp
cmpl $0x0, 0x2c(%rdi)
je 0xa2b7
pushq $-0x1
jmp 0xa3f4
movl %edx, %ebp
movq %rsi, %r14
movq %rdi, %rbx
leaq 0x8(%rsp), %r15
andq $0x0, (%r15)
leaq 0x10(%rsp), %r12
leaq 0x7(%rsp), %rdx
movq %r12, %rdi
callq 0x68b8
movq %r12, %rdi
movq %r15, %rsi
callq 0xa242
movl %eax, %r15d
leaq 0x10(%rsp), %rdi
callq 0x3518
testl %r15d, %r15d
sete %al
movq 0x8(%rsp), %r12
testq %r12, %r12
setne %cl
testb %cl, %al
je 0xa3f2
leaq 0x30(%rbx), %rdi
movq %r14, %rsi
callq 0x34c0
movl %ebp, 0x50(%rbx)
movzwl 0x10(%r12), %eax
movw %ax, 0x1c(%rbx)
leaq 0x20(%rbx), %r14
movq 0x18(%r12), %rax
movq (%rax), %rsi
movslq 0x14(%r12), %rdx
movq %r14, %rdi
callq 0x3210
movzwl 0x50(%rbx), %eax
rolw $0x8, %ax
movw %ax, 0x1e(%rbx)
pushq $0x2
popq %rdi
pushq $0x1
popq %rbp
movl %ebp, %esi
xorl %edx, %edx
callq 0x30f0
movl %eax, 0x8(%rbx)
testl %eax, %eax
js 0xa3f5
movw $0x2, 0xc(%rbx)
andl $0x0, 0x10(%rbx)
leaq 0xc(%rbx), %r15
leaq 0x10(%rsp), %rcx
movl $0x1, (%rcx)
pushq $0x1
popq %rbp
pushq $0x14
popq %rdx
pushq $0x4
popq %r8
movl %eax, %edi
movl %ebp, %esi
callq 0x3480
andw $0x0, 0xe(%rbx)
movl 0x8(%rbx), %edi
pushq $0x10
popq %rdx
movq %r15, %rsi
callq 0x3070
testl %eax, %eax
js 0xa3f5
leaq 0x1c(%rbx), %r15
movzwl 0x10(%r12), %eax
movw %ax, 0x1c(%rbx)
movq 0x18(%r12), %rax
movq (%rax), %rsi
movslq 0x14(%r12), %rdx
movq %r14, %rdi
callq 0x3210
movzwl 0x50(%rbx), %eax
rolw $0x8, %ax
movw %ax, 0x1e(%rbx)
movl 0x8(%rbx), %edi
pushq $0x10
popq %rdx
movq %r15, %rsi
callq 0x3220
testl %eax, %eax
js 0xa3f5
movl $0x1, 0x2c(%rbx)
xorl %ebp, %ebp
jmp 0xa3f5
pushq $0x1
popq %rbp
movl %ebp, %eax
addq $0x30, %rsp
popq %rbx
popq %r12
popq %r14
popq %r15
popq %rbp
retq
movq %rax, %rbx
leaq 0x10(%rsp), %rdi
callq 0x3518
movq %rbx, %rdi
callq 0x3470
nop