// File: /workspace/github_cpp_makefile_star2_O2/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 %r12
pushq %rbx
subq $0x20, %rsp
pushq $0x2
popq %rdi
pushq $0x1
popq %rbx
movl %ebx, %esi
xorl %edx, %edx
callq 0x30c0
leaq 0x8d1e(%rip), %r14      # 0x14300
movl %eax, (%r14)
cmpl $-0x1, %eax
je 0xb6d0
pushq $0x4
popq %rsi
movl %eax, %edi
movl $0x800, %edx            # imm = 0x800
xorl %eax, %eax
callq 0x31f0
cmpl $-0x1, %eax
je 0xb6ec
leaq -0x30(%rbp), %rcx
andq $0x0, (%rcx)
movl (%r14), %edi
pushq $0x1
popq %rsi
pushq $0xd
popq %rdx
pushq $0x8
popq %r8
callq 0x3510
cmpl $-0x1, %eax
je 0xb71c
leaq -0x24(%rbp), %rcx
movl $0x1, (%rcx)
movl (%r14), %edi
pushq $0x1
popq %rsi
pushq $0x2
popq %rdx
pushq $0x4
popq %r8
callq 0x3510
cmpl $-0x1, %eax
je 0xb74c
movl $0xa00f0002, %eax       # imm = 0xA00F0002
leaq -0x40(%rbp), %rsi
movq %rax, (%rsi)
movl (%r14), %edi
pushq $0x10
popq %rdx
callq 0x3070
cmpl $-0x1, %eax
je 0xb768
movl (%r14), %edi
movl $0x1000, %esi           # imm = 0x1000
callq 0x3380
cmpl $-0x1, %eax
je 0xb784
xorl %ebx, %ebx
xorl %edi, %edi
callq 0x32f0
leaq 0x9172(%rip), %rcx      # 0x14808
movq %rax, (%rcx)
leaq -0x138(%rip), %r14      # 0xb568
pushq $0x2
popq %rdi
movq %r14, %rsi
callq 0x33a0
pushq $0xf
popq %rdi
movq %r14, %rsi
callq 0x33a0
pushq $0x1
popq %rdi
movq %r14, %rsi
callq 0x33a0
movl %ebx, %eax
addq $0x20, %rsp
popq %rbx
popq %r12
popq %r14
popq %r15
popq %rbp
retq
pushq $0x10
popq %rdi
callq 0x31b0
movq %rax, %r12
leaq 0x22b9(%rip), %rsi      # 0xd99b
movq %rax, %rdi
callq 0x3140
jmp 0xb706
pushq $0x10
popq %rdi
callq 0x31b0
movq %rax, %r12
leaq 0x22b5(%rip), %rsi      # 0xd9b3
movq %rax, %rdi
callq 0x3140
movq 0x88e3(%rip), %rsi      # 0x13ff0
movq 0x88a4(%rip), %rdx      # 0x13fb8
movq %r12, %rdi
callq 0x34e0
pushq $0x10
popq %rdi
callq 0x31b0
movq %rax, %r12
leaq 0x229d(%rip), %rsi      # 0xd9cb
movq %rax, %rdi
callq 0x3140
movq 0x88b3(%rip), %rsi      # 0x13ff0
movq 0x8874(%rip), %rdx      # 0x13fb8
movq %r12, %rdi
callq 0x34e0
pushq $0x10
popq %rdi
callq 0x31b0
movq %rax, %r12
leaq 0x2284(%rip), %rsi      # 0xd9e2
movq %rax, %rdi
callq 0x3140
jmp 0xb79e
pushq $0x10
popq %rdi
callq 0x31b0
movq %rax, %r12
leaq 0x2282(%rip), %rsi      # 0xd9fc
movq %rax, %rdi
callq 0x3140
jmp 0xb79e
pushq $0x10
popq %rdi
callq 0x31b0
movq %rax, %r12
leaq 0x226b(%rip), %rsi      # 0xda01
movq %rax, %rdi
callq 0x3140
movq 0x884b(%rip), %rsi      # 0x13ff0
movq 0x880c(%rip), %rdx      # 0x13fb8
movq %r12, %rdi
callq 0x34e0
jmp 0xb7c0
jmp 0xb7c0
jmp 0xb7c0
jmp 0xb7d2
jmp 0xb7c0
jmp 0xb7c0
movq %rdx, %r15
movq %rax, %r14
movq %r12, %rdi
callq 0x3270
jmp 0xb7d8
jmp 0xb7d2
movq %rdx, %r15
movq %rax, %r14
cmpl $0x1, %r15d
jne 0xb827
movq %r14, %rdi
callq 0x3150
movq %rax, %r14
movq 0x87f8(%rip), %rdi      # 0x13fe8
leaq 0x2211(%rip), %rsi      # 0xda08
callq 0x3310
movq %rax, %rdi
callq 0x31a0
movq (%r14), %rax
movq %r14, %rdi
callq *0x10(%rax)
movq %rax, %rdi
callq 0x32a0
callq 0x34b0
jmp 0xb6c1
movq %rax, %r14
callq 0x34b0
movq %r14, %rdi
callq 0x3500
movq %rax, %rdi
callq 0x5618