// File: /workspace/github_cpp_makefile_star2_O1/nickgammon[P]tinymudserver/comms.cpp
// Function: InitComms()
int InitComms ()
  {
  struct sockaddr_in sa;
    
  try
    {
  
    // Create the control socket
    if ( (iControl = socket (AF_INET, SOCK_STREAM, 0)) == -1)
      throw runtime_error ("creating control socket");
    
    // make sure socket doesn't block
    if (fcntl( iControl, F_SETFL, FNDELAY ) == -1)
      throw runtime_error ("fcntl on control socket");
  
    struct linger ld = linger ();  // zero it
  
    // Don't allow closed sockets to linger
    if (setsockopt( iControl, SOL_SOCKET, SO_LINGER,
                    (char *) &ld, sizeof ld ) == -1)
      throw runtime_error ("setsockopt (SO_LINGER)");

    int x = 1;

    // Allow address reuse 
    if (setsockopt( iControl, SOL_SOCKET, SO_REUSEADDR,
                   (char *) &x, sizeof x ) == -1)
      throw runtime_error ("setsockopt (SO_REUSEADDR)");
    
    sa.sin_family       = AF_INET;
    sa.sin_port         = htons (PORT);
    sa.sin_addr.s_addr  = INADDR_ANY;   /* change to listen on a specific adapter */
  
    // bind the socket to our connection port
    if ( bind (iControl, (struct sockaddr *) &sa, sizeof sa) == -1)
      throw runtime_error ("bind");
    
    // listen for connections
  
    if (listen (iControl, SOMAXCONN) == -1)   // SOMAXCONN is the backlog count
      throw runtime_error ("listen");
  
    tLastMessage = time (NULL);
    }  // end of try block
    
  // problem?
  catch (runtime_error & e)
    {
    cerr << "Cannot initialise comms ..." << endl;
    perror (e.what ());
    return 1;    
    }

  // standard termination signals
  signal (SIGINT,  bailout);
  signal (SIGTERM, bailout);
  signal (SIGHUP,  bailout);

  return 0;
  }

// Assembly:
pushq %rbp
movq %rsp, %rbp
pushq %r15
pushq %r14
pushq %rbx
subq $0x28, %rsp
movl $0x2, %edi
movl $0x1, %esi
xorl %edx, %edx
callq 0x30b0
leaq 0x85c6(%rip), %rbx      # 0x16300
movl %eax, (%rbx)
cmpl $-0x1, %eax
je 0xde3a
movl %eax, %edi
movl $0x4, %esi
movl $0x800, %edx            # imm = 0x800
xorl %eax, %eax
callq 0x31c0
cmpl $-0x1, %eax
je 0xde58
leaq -0x28(%rbp), %rcx
movq $0x0, (%rcx)
movl (%rbx), %edi
movl $0x1, %esi
movl $0xd, %edx
movl $0x8, %r8d
callq 0x3540
cmpl $-0x1, %eax
je 0xde8a
leaq -0x1c(%rbp), %rcx
movl $0x1, (%rcx)
movl (%rbx), %edi
movl $0x1, %esi
movl $0x2, %edx
movl $0x4, %r8d
callq 0x3540
cmpl $-0x1, %eax
je 0xdebc
movl $0xa00f0002, %eax       # imm = 0xA00F0002
leaq -0x38(%rbp), %rsi
movq %rax, (%rsi)
movl (%rbx), %edi
movl $0x10, %edx
callq 0x3080
cmpl $-0x1, %eax
je 0xdeda
movl (%rbx), %edi
movl $0x1000, %esi           # imm = 0x1000
callq 0x3350
cmpl $-0x1, %eax
je 0xdef8
xorl %ebx, %ebx
xorl %edi, %edi
callq 0x32b0
leaq 0x8a0c(%rip), %rcx      # 0x16808
movq %rax, (%rcx)
leaq -0x196(%rip), %r14      # 0xdc70
movl $0x2, %edi
movq %r14, %rsi
callq 0x3390
movl $0xf, %edi
movq %r14, %rsi
callq 0x3390
movl $0x1, %edi
movq %r14, %rsi
callq 0x3390
movl %ebx, %eax
addq $0x28, %rsp
popq %rbx
popq %r14
popq %r15
popq %rbp
retq
movl $0x10, %edi
callq 0x3180
movq %rax, %r15
leaq 0x2b5f(%rip), %rsi      # 0x109ad
movq %rax, %rdi
callq 0x3130
jmp 0xde74
movl $0x10, %edi
callq 0x3180
movq %rax, %r15
leaq 0x2b59(%rip), %rsi      # 0x109c5
movq %rax, %rdi
callq 0x3130
movq 0x8175(%rip), %rsi      # 0x15ff0
movq 0x812e(%rip), %rdx      # 0x15fb0
movq %r15, %rdi
callq 0x3500
movl $0x10, %edi
callq 0x3180
movq %rax, %r15
leaq 0x2b3f(%rip), %rsi      # 0x109dd
movq %rax, %rdi
callq 0x3130
movq 0x8143(%rip), %rsi      # 0x15ff0
movq 0x80fc(%rip), %rdx      # 0x15fb0
movq %r15, %rdi
callq 0x3500
movl $0x10, %edi
callq 0x3180
movq %rax, %r15
leaq 0x2b24(%rip), %rsi      # 0x109f4
movq %rax, %rdi
callq 0x3130
jmp 0xdf14
movl $0x10, %edi
callq 0x3180
movq %rax, %r15
leaq 0x2b20(%rip), %rsi      # 0x10a0e
movq %rax, %rdi
callq 0x3130
jmp 0xdf14
movl $0x10, %edi
callq 0x3180
movq %rax, %r15
leaq 0x2b07(%rip), %rsi      # 0x10a13
movq %rax, %rdi
callq 0x3130
movq 0x80d5(%rip), %rsi      # 0x15ff0
movq 0x808e(%rip), %rdx      # 0x15fb0
movq %r15, %rdi
callq 0x3500
jmp 0xdf36
jmp 0xdf36
jmp 0xdf36
jmp 0xdf48
jmp 0xdf36
jmp 0xdf36
movq %rdx, %r14
movq %rax, %rbx
movq %r15, %rdi
callq 0x3250
jmp 0xdf4e
jmp 0xdf48
movq %rdx, %r14
movq %rax, %rbx
cmpl $0x1, %r14d
jne 0xdf9d
movq %rbx, %rdi
callq 0x3140
movq %rax, %rbx
movq 0x8082(%rip), %rdi      # 0x15fe8
leaq 0x2aad(%rip), %rsi      # 0x10a1a
movl $0x1b, %edx
callq 0x3370
movq 0x806a(%rip), %rdi      # 0x15fe8
callq 0x3170
movq %rbx, %rdi
callq 0x35d0
movl $0x1, %ebx
jmp 0xde2d
movq %rax, %rbx
callq 0x34c0
movq %rbx, %rdi
callq 0x3530
movq %rax, %rdi
callq 0x6584