// File: mh0fm4[P]SCDC/src/components/transport/transport_tcp.cc
// Function: scdc_transport_tcp::connect(char const*, int)
scdc_transport_connection_t *scdc_transport_tcp::connect(const char *host, int port)
{
  SCDC_TRACE("connect: host: '" << host << "', port: " << port);

  scdc_transport_tcp_connection_t *tcp_conn = new scdc_transport_tcp_connection_t(this);

  tcp_conn->socket = ::socket(AF_INET, SOCK_STREAM, 0);
  if (tcp_conn->socket == -1)
  {
    SCDC_ERROR("connect: creating client socket failed: '" << strerror(errno) << "'");
    return 0;
  }

  struct hostent *h = gethostbyname(host);
  if (!h)
  {
    SCDC_ERROR("connect: gethostbyname failed: '" << strerror(h_errno) << "'");
    return 0;
  }
  
  struct sockaddr_in saddr;
  memset(&saddr, 0, sizeof(saddr));

  saddr.sin_family = AF_INET;
  saddr.sin_addr = *((struct in_addr **) h->h_addr_list)[0];
  saddr.sin_port = htons(port);

  int r;
  bool except = false;

#if SCDC_TRANSPORT_TCP_USE_SHUTDOWN_EXCEPTION
  try
  {
#endif
    r = ::connect(tcp_conn->socket, (struct sockaddr *) &saddr, sizeof(saddr));

#if SCDC_TRANSPORT_TCP_USE_SHUTDOWN_EXCEPTION
  } catch (const scdc_transport_tcp_shutdown_ex &ex)
  {
    SCDC_INFO("connect: catch shutdown exception");
    r = -1;
    except = true;
  }
#endif

  if (r == -1)
  {
    if (!except) SCDC_FAIL("connect: connecting to server failed: '" << strerror(errno) << "'");
    ::close(tcp_conn->socket);
    delete tcp_conn;
    return 0;
  }

  return scdc_transport::connect(tcp_conn);
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
subq $0x90, %rsp
movq %rdi, -0x10(%rbp)
movq %rsi, -0x18(%rbp)
movl %edx, -0x1c(%rbp)
movq -0x10(%rbp), %rax
movq %rax, -0x60(%rbp)
jmp 0x9bc20
movl $0x2d8, %edi            # imm = 0x2D8
callq 0xc690
movq -0x60(%rbp), %rsi
movq %rax, %rdi
movq %rdi, %rax
movq %rax, -0x68(%rbp)
callq 0x9c360
jmp 0x9bc3f
movq -0x68(%rbp), %rax
movq %rax, -0x28(%rbp)
movl $0x2, %edi
movl $0x1, %esi
xorl %edx, %edx
callq 0xc130
movl %eax, %ecx
movq -0x28(%rbp), %rax
movl %ecx, 0x2d4(%rax)
movq -0x28(%rbp), %rax
cmpl $-0x1, 0x2d4(%rax)
jne 0x9bd54
jmp 0x9bc77
leaq 0x38a2a(%rip), %rax     # 0xd46a8
movq (%rax), %rdi
callq 0x2e8e0
cmpq $0x0, %rax
je 0x9bd45
leaq 0x38a11(%rip), %rax     # 0xd46a8
movq (%rax), %rdi
callq 0x2e900
leaq 0x38a02(%rip), %rax     # 0xd46a8
movq (%rax), %rdi
callq 0x2e8e0
movq %rax, %rdi
leaq 0xa7f0(%rip), %rsi      # 0xa64a8
callq 0xc670
movq %rax, %rdi
leaq 0xdf32(%rip), %rsi      # 0xa9bf9
callq 0xc670
movq %rax, %rdi
leaq 0xdfea(%rip), %rsi      # 0xa9cc0
callq 0xc670
movq %rax, -0x70(%rbp)
callq 0xc080
movl (%rax), %edi
callq 0xc1d0
movq -0x70(%rbp), %rdi
movq %rax, %rsi
callq 0xc670
movq %rax, %rdi
leaq 0xc6d2(%rip), %rsi      # 0xa83d3
callq 0xc670
movq %rax, %rdi
movq 0x382b0(%rip), %rsi     # 0xd3fc0
callq 0xc6d0
leaq 0x3898c(%rip), %rax     # 0xd46a8
movq (%rax), %rdi
callq 0x2e920
jmp 0x9bd45
movq -0x68(%rbp), %rdi
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x30(%rbp)
movl %eax, -0x34(%rbp)
movl $0x2d8, %esi            # imm = 0x2D8
callq 0xc6a0
jmp 0x9bfaa
jmp 0x9bd47
movq $0x0, -0x8(%rbp)
jmp 0x9bf9d
movq -0x18(%rbp), %rdi
callq 0xc7c0
movq %rax, -0x40(%rbp)
cmpq $0x0, -0x40(%rbp)
jne 0x9be2a
jmp 0x9bd6e
leaq 0x38933(%rip), %rax     # 0xd46a8
movq (%rax), %rdi
callq 0x2e8e0
cmpq $0x0, %rax
je 0x9be1b
leaq 0x3891a(%rip), %rax     # 0xd46a8
movq (%rax), %rdi
callq 0x2e900
leaq 0x3890b(%rip), %rax     # 0xd46a8
movq (%rax), %rdi
callq 0x2e8e0
movq %rax, %rdi
leaq 0xa6f9(%rip), %rsi      # 0xa64a8
callq 0xc670
movq %rax, %rdi
leaq 0xde3b(%rip), %rsi      # 0xa9bf9
callq 0xc670
movq %rax, %rdi
leaq 0xdf1d(%rip), %rsi      # 0xa9cea
callq 0xc670
movq %rax, -0x78(%rbp)
callq 0xca90
movl (%rax), %edi
callq 0xc1d0
movq -0x78(%rbp), %rdi
movq %rax, %rsi
callq 0xc670
movq %rax, %rdi
leaq 0xc5db(%rip), %rsi      # 0xa83d3
callq 0xc670
movq %rax, %rdi
movq 0x381b9(%rip), %rsi     # 0xd3fc0
callq 0xc6d0
leaq 0x38895(%rip), %rax     # 0xd46a8
movq (%rax), %rdi
callq 0x2e920
jmp 0x9be1d
movq $0x0, -0x8(%rbp)
jmp 0x9bf9d
leaq -0x50(%rbp), %rdi
xorl %esi, %esi
movl $0x10, %edx
callq 0xc350
movw $0x2, -0x50(%rbp)
movq -0x40(%rbp), %rax
movq 0x18(%rax), %rax
movq (%rax), %rax
movl (%rax), %eax
movl %eax, -0x4c(%rbp)
movl -0x1c(%rbp), %eax
movzwl %ax, %edi
callq 0xc090
movw %ax, -0x4e(%rbp)
movb $0x0, -0x55(%rbp)
movq -0x28(%rbp), %rax
movl 0x2d4(%rax), %edi
leaq -0x50(%rbp), %rsi
movl $0x10, %edx
callq 0xc540
movl %eax, -0x54(%rbp)
cmpl $-0x1, -0x54(%rbp)
jne 0x9bf8c
testb $0x1, -0x55(%rbp)
jne 0x9bf45
jmp 0x9be94
leaq 0x3880d(%rip), %rax     # 0xd46a8
movq (%rax), %rdi
callq 0x257e0
cmpq $0x0, %rax
je 0x9bf41
leaq 0x387f4(%rip), %rax     # 0xd46a8
movq (%rax), %rdi
callq 0x25800
leaq 0x387e5(%rip), %rax     # 0xd46a8
movq (%rax), %rdi
callq 0x257e0
movq %rax, %rdi
leaq 0xa2db(%rip), %rsi      # 0xa61b0
callq 0xc670
movq %rax, %rdi
leaq 0xdd15(%rip), %rsi      # 0xa9bf9
callq 0xc670
movq %rax, %rdi
leaq 0xde18(%rip), %rsi      # 0xa9d0b
callq 0xc670
movq %rax, -0x80(%rbp)
callq 0xc080
movl (%rax), %edi
callq 0xc1d0
movq -0x80(%rbp), %rdi
movq %rax, %rsi
callq 0xc670
movq %rax, %rdi
leaq 0xc4b5(%rip), %rsi      # 0xa83d3
callq 0xc670
movq %rax, %rdi
movq 0x38093(%rip), %rsi     # 0xd3fc0
callq 0xc6d0
leaq 0x3876f(%rip), %rax     # 0xd46a8
movq (%rax), %rdi
callq 0x25820
jmp 0x9bf43
jmp 0x9bf45
movq -0x28(%rbp), %rax
movl 0x2d4(%rax), %edi
callq 0xccd0
movq -0x28(%rbp), %rax
movq %rax, -0x88(%rbp)
cmpq $0x0, %rax
je 0x9bf82
movq -0x88(%rbp), %rdi
callq 0x9c3a0
movq -0x88(%rbp), %rdi
movl $0x2d8, %esi            # imm = 0x2D8
callq 0xc6a0
movq $0x0, -0x8(%rbp)
jmp 0x9bf9d
movq -0x60(%rbp), %rdi
movq -0x28(%rbp), %rsi
callq 0x98d80
movq %rax, -0x8(%rbp)
movq -0x8(%rbp), %rax
addq $0x90, %rsp
popq %rbp
retq
movq -0x30(%rbp), %rdi
callq 0xcba0
nopw %cs:(%rax,%rax)