// File: /workspace/github_cpp_makefile_star2_O3/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 0x864a(%rip), %rbx      # 0x16300
movl %eax, (%rbx)
cmpl $-0x1, %eax
je 0xddb6
movl %eax, %edi
movl $0x4, %esi
movl $0x800, %edx            # imm = 0x800
xorl %eax, %eax
callq 0x31c0
cmpl $-0x1, %eax
je 0xddd4
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 0xde06
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 0xde38
movl $0xa00f0002, %eax       # imm = 0xA00F0002
leaq -0x38(%rbp), %rsi
movq %rax, (%rsi)
movl (%rbx), %edi
movl $0x10, %edx
callq 0x3080
cmpl $-0x1, %eax
je 0xde56
movl (%rbx), %edi
movl $0x1000, %esi           # imm = 0x1000
callq 0x3350
cmpl $-0x1, %eax
je 0xde74
xorl %ebx, %ebx
xorl %edi, %edi
callq 0x32b0
leaq 0x8a90(%rip), %rcx      # 0x16808
movq %rax, (%rcx)
leaq -0x196(%rip), %r14      # 0xdbec
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 0x2be3(%rip), %rsi      # 0x109ad
movq %rax, %rdi
callq 0x3130
jmp 0xddf0
movl $0x10, %edi
callq 0x3180
movq %rax, %r15
leaq 0x2bdd(%rip), %rsi      # 0x109c5
movq %rax, %rdi
callq 0x3130
movq 0x81f9(%rip), %rsi      # 0x15ff0
movq 0x81b2(%rip), %rdx      # 0x15fb0
movq %r15, %rdi
callq 0x3500
movl $0x10, %edi
callq 0x3180
movq %rax, %r15
leaq 0x2bc3(%rip), %rsi      # 0x109dd
movq %rax, %rdi
callq 0x3130
movq 0x81c7(%rip), %rsi      # 0x15ff0
movq 0x8180(%rip), %rdx      # 0x15fb0
movq %r15, %rdi
callq 0x3500
movl $0x10, %edi
callq 0x3180
movq %rax, %r15
leaq 0x2ba8(%rip), %rsi      # 0x109f4
movq %rax, %rdi
callq 0x3130
jmp 0xde90
movl $0x10, %edi
callq 0x3180
movq %rax, %r15
leaq 0x2ba4(%rip), %rsi      # 0x10a0e
movq %rax, %rdi
callq 0x3130
jmp 0xde90
movl $0x10, %edi
callq 0x3180
movq %rax, %r15
leaq 0x2b8b(%rip), %rsi      # 0x10a13
movq %rax, %rdi
callq 0x3130
movq 0x8159(%rip), %rsi      # 0x15ff0
movq 0x8112(%rip), %rdx      # 0x15fb0
movq %r15, %rdi
callq 0x3500
jmp 0xdeb2
jmp 0xdeb2
jmp 0xdeb2
jmp 0xdec4
jmp 0xdeb2
jmp 0xdeb2
movq %rdx, %r14
movq %rax, %rbx
movq %r15, %rdi
callq 0x3250
jmp 0xdeca
jmp 0xdec4
movq %rdx, %r14
movq %rax, %rbx
cmpl $0x1, %r14d
jne 0xdf19
movq %rbx, %rdi
callq 0x3140
movq %rax, %rbx
movq 0x8106(%rip), %rdi      # 0x15fe8
leaq 0x2b31(%rip), %rsi      # 0x10a1a
movl $0x1b, %edx
callq 0x3370
movq 0x80ee(%rip), %rdi      # 0x15fe8
callq 0x3170
movq %rbx, %rdi
callq 0x35d0
movl $0x1, %ebx
jmp 0xdda9
movq %rax, %rbx
callq 0x34c0
movq %rbx, %rdi
callq 0x3530
movq %rax, %rdi
callq 0x6550