// File: zeeev[P]DASVC/bamtools/src/api/internal/io/TcpSocket_p.cpp
// Function: BamTools::Internal::TcpSocket::ConnectImpl(BamTools::Internal::HostInfo const&, std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char>> const&, BamTools::IBamIODevice::OpenMode)
bool TcpSocket::ConnectImpl(const HostInfo& hostInfo,
                            const std::string& port,
                            IBamIODevice::OpenMode mode)
{
    // skip if we're already connected
    if ( m_state == TcpSocket::ConnectedState ) {
        m_error = TcpSocket::SocketResourceError;
        m_errorString = "socket already connected";
        return false;
    }

    // reset socket state
    m_hostName   = hostInfo.HostName();
    m_mode       = mode;
    m_state      = TcpSocket::UnconnectedState;
    m_error      = TcpSocket::NoError;
//    m_localPort  = 0;
    m_remotePort = 0;
//    m_localAddress.Clear();
    m_remoteAddress.Clear();
    m_readBuffer.Clear();

    // fetch candidate addresses for requested host
    vector<HostAddress> addresses = hostInfo.Addresses();
    if ( addresses.empty() ) {
        m_error = TcpSocket::HostNotFoundError;
        m_errorString = "no IP addresses found for host";
        return false;
    }

    // convert port string to integer
    stringstream ss(port);
    uint16_t portNumber(0);
    ss >> portNumber;

    // iterate through adddresses
    vector<HostAddress>::const_iterator addrIter = addresses.begin();
    vector<HostAddress>::const_iterator addrEnd  = addresses.end();
    for ( ; addrIter != addrEnd; ++addrIter) {
        const HostAddress& addr = (*addrIter);

        // try to initialize socket engine with this address
        if ( !InitializeSocketEngine(addr.GetProtocol()) ) {
            // failure to initialize is OK here
            // we'll just try the next available address
            continue;
        }

        // attempt actual connection
        if ( m_engine->Connect(addr, portNumber) ) {

            // if connection successful, update our state & return true
            m_mode = mode;
//            m_localAddress  = m_engine->GetLocalAddress();
//            m_localPort     = m_engine->GetLocalPort();
            m_remoteAddress = m_engine->GetRemoteAddress();
            m_remotePort    = m_engine->GetRemotePort();
            m_cachedSocketDescriptor = m_engine->GetSocketDescriptor();
            m_state = TcpSocket::ConnectedState;
            return true;
        }
    }

    // if we get here, no connection could be made
    m_error = TcpSocket::HostNotFoundError;
    m_errorString = "could not connect to any host addresses";
    return false;
}

// Assembly:
subq $0x2d8, %rsp            # imm = 0x2D8
movq %rdi, 0x2c8(%rsp)
movq %rsi, 0x2c0(%rsp)
movq %rdx, 0x2b8(%rsp)
movl %ecx, 0x2b4(%rsp)
movq 0x2c8(%rsp), %rax
movq %rax, 0x60(%rsp)
cmpl $0x1, 0xfc(%rax)
jne 0xdfc1b
movq 0x60(%rsp), %rdi
movl $0x4, 0xf8(%rdi)
addq $0x100, %rdi            # imm = 0x100
leaq 0xc6fd(%rip), %rsi      # 0xec306
callq 0x78d10
movb $0x0, 0x2d7(%rsp)
jmp 0xe002a
movq 0x2c0(%rsp), %rsi
leaq 0x290(%rsp), %rdi
movq %rdi, 0x58(%rsp)
callq 0x715f0
movq 0x60(%rsp), %rdi
movq 0x58(%rsp), %rsi
addq $0x8, %rdi
callq 0x71240
jmp 0xdfc4a
leaq 0x290(%rsp), %rdi
callq 0x7cce8
movq 0x60(%rsp), %rdi
movl 0x2b4(%rsp), %eax
movl %eax, (%rdi)
movl $0x0, 0xfc(%rdi)
movl $0xfffffffe, 0xf8(%rdi) # imm = 0xFFFFFFFE
movw $0x0, 0x28(%rdi)
addq $0x30, %rdi
callq 0x7c220
movq 0x60(%rsp), %rdi
subq $-0x80, %rdi
callq 0x77c40
movq 0x2c0(%rsp), %rsi
leaq 0x268(%rsp), %rdi
movq %rdi, 0x48(%rsp)
callq 0x73960
movq 0x48(%rsp), %rdi
callq 0x7b670
movb %al, 0x57(%rsp)
jmp 0xdfcc0
movb 0x57(%rsp), %al
testb $0x1, %al
jne 0xdfcca
jmp 0xdfd47
movq 0x60(%rsp), %rdi
movl $0x2, 0xf8(%rdi)
addq $0x100, %rdi            # imm = 0x100
leaq 0xc638(%rip), %rsi      # 0xec31f
callq 0x78d10
jmp 0xdfcee
movb $0x0, 0x2d7(%rsp)
movl $0x1, 0x264(%rsp)
jmp 0xe000a
movq %rax, %rcx
movl %edx, %eax
movq %rcx, 0x288(%rsp)
movl %eax, 0x284(%rsp)
leaq 0x290(%rsp), %rdi
callq 0x7cce8
jmp 0xdfd29
jmp 0xe003b
movq %rax, %rcx
movl %edx, %eax
movq %rcx, 0x288(%rsp)
movl %eax, 0x284(%rsp)
jmp 0xe0019
movq 0x2b8(%rsp), %rax
movq %rax, 0x38(%rsp)
movl $0x10, %edi
movl $0x8, %esi
callq 0x7b8d0
movl %eax, 0x44(%rsp)
jmp 0xdfd69
movl 0x44(%rsp), %edx
movq 0x38(%rsp), %rsi
leaq 0xd8(%rsp), %rdi
callq 0x7a4b0
jmp 0xdfd81
movw $0x0, 0xd6(%rsp)
leaq 0xd8(%rsp), %rdi
leaq 0xd6(%rsp), %rsi
callq 0x73f80
jmp 0xdfda2
leaq 0x268(%rsp), %rdi
callq 0x790b0
movq %rax, 0x30(%rsp)
jmp 0xdfdb6
movq 0x30(%rsp), %rax
movq %rax, 0xc0(%rsp)
leaq 0xc8(%rsp), %rdi
leaq 0xc0(%rsp), %rsi
callq 0x7ca20
jmp 0xdfdda
leaq 0x268(%rsp), %rdi
callq 0x72960
movq %rax, 0x28(%rsp)
jmp 0xdfdee
movq 0x28(%rsp), %rax
movq %rax, 0xb0(%rsp)
leaq 0xb8(%rsp), %rdi
leaq 0xb0(%rsp), %rsi
callq 0x7ca20
jmp 0xdfe12
jmp 0xdfe14
leaq 0xc8(%rsp), %rdi
leaq 0xb8(%rsp), %rsi
callq 0x7bb90
movb %al, 0x27(%rsp)
jmp 0xdfe2f
movb 0x27(%rsp), %al
testb $0x1, %al
jne 0xdfe3c
jmp 0xdffb1
leaq 0xc8(%rsp), %rdi
callq 0x717a0
movq %rax, 0x18(%rsp)
jmp 0xdfe50
movq 0x18(%rsp), %rax
movq %rax, 0xa8(%rsp)
movq 0xa8(%rsp), %rdi
callq 0x7ba20
movl %eax, 0x14(%rsp)
jmp 0xdfe70
movl 0x14(%rsp), %esi
movq 0x60(%rsp), %rdi
callq 0x7bdd0
movb %al, 0x13(%rsp)
jmp 0xdfe84
movb 0x13(%rsp), %al
testb $0x1, %al
jne 0xdfeac
jmp 0xdfe8e
jmp 0xdff9d
movq %rax, %rcx
movl %edx, %eax
movq %rcx, 0x288(%rsp)
movl %eax, 0x284(%rsp)
jmp 0xdfff9
movq 0x60(%rsp), %rax
movq 0x70(%rax), %rdi
movq 0xa8(%rsp), %rsi
movzwl 0xd6(%rsp), %edx
callq 0x7a520
movb %al, 0x12(%rsp)
jmp 0xdfed0
movb 0x12(%rsp), %al
testb $0x1, %al
jne 0xdfedd
jmp 0xdff9b
movq 0x60(%rsp), %rax
movl 0x2b4(%rsp), %ecx
movl %ecx, (%rax)
movq 0x70(%rax), %rsi
leaq 0x68(%rsp), %rdi
callq 0x71ba0
jmp 0xdfefb
movq 0x60(%rsp), %rdi
addq $0x30, %rdi
leaq 0x68(%rsp), %rsi
callq 0x7a2b0
jmp 0xdff10
leaq 0x68(%rsp), %rdi
callq 0x72a20
jmp 0xdff1c
movq 0x60(%rsp), %rax
movq 0x70(%rax), %rdi
callq 0x71260
movw %ax, 0x10(%rsp)
jmp 0xdff31
movq 0x60(%rsp), %rax
movw 0x10(%rsp), %cx
movw %cx, 0x28(%rax)
movq 0x70(%rax), %rdi
callq 0x77840
movl %eax, 0xc(%rsp)
jmp 0xdff4e
movq 0x60(%rsp), %rax
movl 0xc(%rsp), %ecx
movl %ecx, 0x78(%rax)
movl $0x1, 0xfc(%rax)
movb $0x1, 0x2d7(%rsp)
movl $0x1, 0x264(%rsp)
jmp 0xdffe8
movq %rax, %rcx
movl %edx, %eax
movq %rcx, 0x288(%rsp)
movl %eax, 0x284(%rsp)
leaq 0x68(%rsp), %rdi
callq 0x72a20
jmp 0xdff99
jmp 0xdfff9
jmp 0xdff9d
leaq 0xc8(%rsp), %rdi
callq 0x79ae0
jmp 0xdffac
jmp 0xdfe14
movq 0x60(%rsp), %rdi
movl $0x2, 0xf8(%rdi)
addq $0x100, %rdi            # imm = 0x100
leaq 0xc370(%rip), %rsi      # 0xec33e
callq 0x78d10
jmp 0xdffd5
movb $0x0, 0x2d7(%rsp)
movl $0x1, 0x264(%rsp)
leaq 0xd8(%rsp), %rdi
callq 0x74f60
jmp 0xdfff7
jmp 0xe000a
leaq 0xd8(%rsp), %rdi
callq 0x74f60
jmp 0xe0008
jmp 0xe0019
leaq 0x268(%rsp), %rdi
callq 0x797f0
jmp 0xe002a
leaq 0x268(%rsp), %rdi
callq 0x797f0
jmp 0xe0028
jmp 0xe003b
movb 0x2d7(%rsp), %al
andb $0x1, %al
addq $0x2d8, %rsp            # imm = 0x2D8
retq
movq 0x288(%rsp), %rdi
callq 0x7b550
movq %rax, %rdi
callq 0x88ef0