// File: zeeev[P]DASVC/bamtools/src/api/internal/io/BamFtp_p.cpp
// Function: BamTools::Internal::BamFtp::ConnectDataSocket()
bool BamFtp::ConnectDataSocket(void) {

    // failure if can't connect to command socket first
    if ( !m_commandSocket->IsConnected() ) {
        if ( !ConnectCommandSocket() )
            return false;
    }

    // make sure we're starting with a fresh data channel
    if ( m_dataSocket->IsConnected() )
        m_dataSocket->DisconnectFromHost();

    // send passive connection command
    const string passiveCommand = PASV_CMD + FTP_NEWLINE;
    if ( !SendCommand(passiveCommand, true) ) {
        // TODO: set error string
        return false;
    }

    // retrieve passive connection port
    if ( !ParsePassiveResponse() ) {
        // TODO: set error string
        return false;
    }

    // set up restart command (tell server where to start fetching bytes from)
    if ( m_filePosition >= 0 ) {

        stringstream fpStream("");
        fpStream << m_filePosition;
        string restartCommand = REST_CMD + CMD_SEPARATOR + fpStream.str() + FTP_NEWLINE;
        if ( !SendCommand(restartCommand, true) ) {
            // TODO: set error string
            return false;
        }
    }

    // main file retrieval request
    string retrieveCommand = RETR_CMD + CMD_SEPARATOR + m_filename + FTP_NEWLINE;
    if ( !SendCommand(retrieveCommand, false) ) {
        // TODO: set error string
        return false;
    }

    // make data channel connection
    if ( !m_dataSocket->ConnectToHost(m_dataHostname, m_dataPort) ) {
        // TODO: set error string
        return false;
    }

    // fetch intial reply from server
    if ( !ReceiveReply() ) {
        // TODO: set error string
        m_dataSocket->DisconnectFromHost();
        return false;
    }

    // make sure we have reply code 150 (all good)
    if ( !startsWith(m_response, "150") ) {
        // TODO: set error string
        m_dataSocket->DisconnectFromHost();
        return false;
    }

    // return success
    return true;
}

// Assembly:
subq $0x348, %rsp            # imm = 0x348
movq %rdi, 0x338(%rsp)
movq 0x338(%rsp), %rax
movq %rax, 0x48(%rsp)
movq 0x30(%rax), %rdi
callq 0x75cd0
testb $0x1, %al
jne 0xc7e56
movq 0x48(%rsp), %rdi
callq 0x71db0
testb $0x1, %al
jne 0xc7e54
movb $0x0, 0x347(%rsp)
jmp 0xc84c4
jmp 0xc7e56
movq 0x48(%rsp), %rax
movq 0x38(%rax), %rdi
callq 0x75cd0
testb $0x1, %al
jne 0xc7e6a
jmp 0xc7e78
movq 0x48(%rsp), %rax
movq 0x38(%rax), %rdi
callq 0x7b600
leaq 0x50e11(%rip), %rsi     # 0x118c90
leaq 0x50d4a(%rip), %rdx     # 0x118bd0
leaq 0x318(%rsp), %rdi
movq %rdi, 0x38(%rsp)
callq 0x7c720
movq 0x48(%rsp), %rdi
movq 0x38(%rsp), %rsi
movl $0x1, %edx
callq 0x798e0
movb %al, 0x47(%rsp)
jmp 0xc7eb2
movb 0x47(%rsp), %al
testb $0x1, %al
jne 0xc7eed
jmp 0xc7ebc
movb $0x0, 0x347(%rsp)
movl $0x1, 0x308(%rsp)
jmp 0xc84a4
movq %rax, %rcx
movl %edx, %eax
movq %rcx, 0x310(%rsp)
movl %eax, 0x30c(%rsp)
jmp 0xc84b3
movq 0x48(%rsp), %rdi
callq 0x76b10
movb %al, 0x37(%rsp)
jmp 0xc7efd
movb 0x37(%rsp), %al
testb $0x1, %al
jne 0xc7f1f
jmp 0xc7f07
movb $0x0, 0x347(%rsp)
movl $0x1, 0x308(%rsp)
jmp 0xc84a4
movq 0x48(%rsp), %rax
cmpq $0x0, 0x118(%rax)
jl 0xc81f3
leaq 0x15f(%rsp), %rdi
movq %rdi, 0x28(%rsp)
callq 0x7b5f0
movq 0x28(%rsp), %rdx
leaq 0x2318c(%rip), %rsi     # 0xeb0dc
leaq 0x160(%rsp), %rdi
callq 0x78e70
jmp 0xc7f5f
movl $0x10, %edi
movl $0x8, %esi
callq 0x7b8d0
movl %eax, 0x24(%rsp)
jmp 0xc7f74
movl 0x24(%rsp), %edx
leaq 0x180(%rsp), %rdi
leaq 0x160(%rsp), %rsi
callq 0x7a4b0
jmp 0xc7f8f
leaq 0x160(%rsp), %rdi
callq 0x7cce8
jmp 0xc7f9e
leaq 0x15f(%rsp), %rdi
callq 0x76f80
movq 0x48(%rsp), %rax
leaq 0x190(%rsp), %rdi
movq 0x118(%rax), %rsi
callq 0x7ba30
jmp 0xc7fc6
leaq 0x50d03(%rip), %rsi     # 0x118cd0
leaq 0xf8(%rsp), %rdi
movl $0x20, %edx
callq 0x74830
jmp 0xc7fe1
leaq 0xd8(%rsp), %rdi
leaq 0x180(%rsp), %rsi
callq 0x76880
jmp 0xc7ff8
leaq 0x118(%rsp), %rdi
leaq 0xf8(%rsp), %rsi
leaq 0xd8(%rsp), %rdx
callq 0x7c720
jmp 0xc8017
leaq 0x50bb2(%rip), %rdx     # 0x118bd0
leaq 0x138(%rsp), %rdi
leaq 0x118(%rsp), %rsi
callq 0x7c720
jmp 0xc8035
leaq 0x118(%rsp), %rdi
callq 0x7cce8
jmp 0xc8044
leaq 0xd8(%rsp), %rdi
callq 0x7cce8
jmp 0xc8053
leaq 0xf8(%rsp), %rdi
callq 0x7cce8
jmp 0xc8062
movq 0x48(%rsp), %rdi
leaq 0x138(%rsp), %rsi
movl $0x1, %edx
callq 0x798e0
movb %al, 0x23(%rsp)
jmp 0xc807f
movb 0x23(%rsp), %al
testb $0x1, %al
jne 0xc81a1
jmp 0xc808d
movb $0x0, 0x347(%rsp)
movl $0x1, 0x308(%rsp)
jmp 0xc81ac
movq %rax, %rcx
movl %edx, %eax
movq %rcx, 0x310(%rsp)
movl %eax, 0x30c(%rsp)
jmp 0xc80e0
movq %rax, %rcx
movl %edx, %eax
movq %rcx, 0x310(%rsp)
movl %eax, 0x30c(%rsp)
leaq 0x160(%rsp), %rdi
callq 0x7cce8
jmp 0xc80de
jmp 0xc80e0
leaq 0x15f(%rsp), %rdi
callq 0x76f80
jmp 0xc84b3
movq %rax, %rcx
movl %edx, %eax
movq %rcx, 0x310(%rsp)
movl %eax, 0x30c(%rsp)
jmp 0xc81df
movq %rax, %rcx
movl %edx, %eax
movq %rcx, 0x310(%rsp)
movl %eax, 0x30c(%rsp)
jmp 0xc816d
movq %rax, %rcx
movl %edx, %eax
movq %rcx, 0x310(%rsp)
movl %eax, 0x30c(%rsp)
jmp 0xc815c
movq %rax, %rcx
movl %edx, %eax
movq %rcx, 0x310(%rsp)
movl %eax, 0x30c(%rsp)
leaq 0x118(%rsp), %rdi
callq 0x7cce8
jmp 0xc815a
jmp 0xc815c
leaq 0xd8(%rsp), %rdi
callq 0x7cce8
jmp 0xc816b
jmp 0xc816d
leaq 0xf8(%rsp), %rdi
callq 0x7cce8
jmp 0xc817c
jmp 0xc81df
movq %rax, %rcx
movl %edx, %eax
movq %rcx, 0x310(%rsp)
movl %eax, 0x30c(%rsp)
leaq 0x138(%rsp), %rdi
callq 0x7cce8
jmp 0xc81dd
movl $0x0, 0x308(%rsp)
leaq 0x138(%rsp), %rdi
callq 0x7cce8
jmp 0xc81bb
leaq 0x180(%rsp), %rdi
callq 0x74f60
jmp 0xc81ca
movl 0x308(%rsp), %eax
testl %eax, %eax
jne 0xc84a4
jmp 0xc81db
jmp 0xc81f3
jmp 0xc81df
leaq 0x180(%rsp), %rdi
callq 0x74f60
jmp 0xc81ee
jmp 0xc84b3
leaq 0x50af6(%rip), %rsi     # 0x118cf0
leaq 0x78(%rsp), %rdi
movl $0x20, %edx
callq 0x74830
jmp 0xc820b
movq 0x48(%rsp), %rdx
addq $0x90, %rdx
leaq 0x98(%rsp), %rdi
leaq 0x78(%rsp), %rsi
callq 0x7c720
jmp 0xc822b
leaq 0x5099e(%rip), %rdx     # 0x118bd0
leaq 0xb8(%rsp), %rdi
leaq 0x98(%rsp), %rsi
callq 0x7c720
jmp 0xc8249
leaq 0x98(%rsp), %rdi
callq 0x7cce8
jmp 0xc8258
leaq 0x78(%rsp), %rdi
callq 0x7cce8
jmp 0xc8264
movq 0x48(%rsp), %rdi
leaq 0xb8(%rsp), %rsi
xorl %edx, %edx
callq 0x798e0
movb %al, 0x22(%rsp)
jmp 0xc827e
movb 0x22(%rsp), %al
testb $0x1, %al
jne 0xc8305
jmp 0xc8288
movb $0x0, 0x347(%rsp)
movl $0x1, 0x308(%rsp)
jmp 0xc8482
movq %rax, %rcx
movl %edx, %eax
movq %rcx, 0x310(%rsp)
movl %eax, 0x30c(%rsp)
jmp 0xc82db
movq %rax, %rcx
movl %edx, %eax
movq %rcx, 0x310(%rsp)
movl %eax, 0x30c(%rsp)
leaq 0x98(%rsp), %rdi
callq 0x7cce8
jmp 0xc82d9
jmp 0xc82db
leaq 0x78(%rsp), %rdi
callq 0x7cce8
jmp 0xc82e7
jmp 0xc84b3
movq %rax, %rcx
movl %edx, %eax
movq %rcx, 0x310(%rsp)
movl %eax, 0x30c(%rsp)
jmp 0xc8493
movq 0x48(%rsp), %rax
movq 0x38(%rax), %rdi
movq %rax, %rsi
addq $0x68, %rsi
movzwl 0x88(%rax), %edx
movl $0x1, %ecx
callq 0x78770
movb %al, 0x21(%rsp)
jmp 0xc832c
movb 0x21(%rsp), %al
testb $0x1, %al
jne 0xc834e
jmp 0xc8336
movb $0x0, 0x347(%rsp)
movl $0x1, 0x308(%rsp)
jmp 0xc8482
movq 0x48(%rsp), %rdi
callq 0x77950
movb %al, 0x20(%rsp)
jmp 0xc835e
movb 0x20(%rsp), %al
testb $0x1, %al
jne 0xc8390
jmp 0xc8368
movq 0x48(%rsp), %rax
movq 0x38(%rax), %rdi
callq 0x7b600
jmp 0xc8378
movb $0x0, 0x347(%rsp)
movl $0x1, 0x308(%rsp)
jmp 0xc8482
movq 0x48(%rsp), %rax
addq $0xf0, %rax
movq %rax, 0x10(%rsp)
leaq 0x57(%rsp), %rdi
movq %rdi, 0x18(%rsp)
callq 0x7b5f0
movq 0x18(%rsp), %rdx
leaq 0x2388d(%rip), %rsi     # 0xebc48
leaq 0x58(%rsp), %rdi
callq 0x78e70
jmp 0xc83c7
movq 0x10(%rsp), %rdi
leaq 0x58(%rsp), %rsi
callq 0xc8a70
movb %al, 0xf(%rsp)
jmp 0xc83dc
movb 0xf(%rsp), %al
xorb $0x1, %al
movb %al, 0xe(%rsp)
leaq 0x58(%rsp), %rdi
callq 0x7cce8
jmp 0xc83f2
leaq 0x57(%rsp), %rdi
callq 0x76f80
movb 0xe(%rsp), %al
testb $0x1, %al
jne 0xc8406
jmp 0xc846f
movq 0x48(%rsp), %rax
movq 0x38(%rax), %rdi
callq 0x7b600
jmp 0xc8416
movb $0x0, 0x347(%rsp)
movl $0x1, 0x308(%rsp)
jmp 0xc8482
movq %rax, %rcx
movl %edx, %eax
movq %rcx, 0x310(%rsp)
movl %eax, 0x30c(%rsp)
jmp 0xc8463
movq %rax, %rcx
movl %edx, %eax
movq %rcx, 0x310(%rsp)
movl %eax, 0x30c(%rsp)
leaq 0x58(%rsp), %rdi
callq 0x7cce8
jmp 0xc8461
jmp 0xc8463
leaq 0x57(%rsp), %rdi
callq 0x76f80
jmp 0xc8493
movb $0x1, 0x347(%rsp)
movl $0x1, 0x308(%rsp)
leaq 0xb8(%rsp), %rdi
callq 0x7cce8
jmp 0xc8491
jmp 0xc84a4
leaq 0xb8(%rsp), %rdi
callq 0x7cce8
jmp 0xc84a2
jmp 0xc84b3
leaq 0x318(%rsp), %rdi
callq 0x7cce8
jmp 0xc84c4
leaq 0x318(%rsp), %rdi
callq 0x7cce8
jmp 0xc84c2
jmp 0xc84d5
movb 0x347(%rsp), %al
andb $0x1, %al
addq $0x348, %rsp            # imm = 0x348
retq
movq 0x310(%rsp), %rdi
callq 0x7b550
movq %rax, %rdi
callq 0x88ef0
nopw (%rax,%rax)