// File: 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
subq $0x80, %rsp
movl $0x2, %edi
movl $0x1, %esi
xorl %edx, %edx
callq 0x4110
leaq 0xf96d(%rip), %rcx      # 0x29420
movl %eax, (%rcx)
cmpl $-0x1, %eax
jne 0x19b23
movl $0x10, %edi
callq 0x4250
movq %rax, %rdi
movq %rdi, %rax
movq %rax, -0x40(%rbp)
leaq 0x2ec6(%rip), %rsi      # 0x1c99b
callq 0x41c0
jmp 0x19adc
movq -0x40(%rbp), %rdi
movq 0xf509(%rip), %rsi      # 0x28ff0
movq 0xf4ca(%rip), %rdx      # 0x28fb8
callq 0x46a0
jmp 0x19ea3
movq -0x40(%rbp), %rdi
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x20(%rbp)
movl %eax, -0x24(%rbp)
callq 0x4350
jmp 0x19db2
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x20(%rbp)
movl %eax, -0x24(%rbp)
jmp 0x19db2
leaq 0xf8f6(%rip), %rax      # 0x29420
movl (%rax), %edi
xorl %eax, %eax
movl $0x4, %esi
movl $0x800, %edx            # imm = 0x800
callq 0x4290
movl %eax, -0x44(%rbp)
jmp 0x19b42
movl -0x44(%rbp), %eax
cmpl $-0x1, %eax
jne 0x19ba2
movl $0x10, %edi
callq 0x4250
movq %rax, %rdi
movq %rdi, %rax
movq %rax, -0x50(%rbp)
leaq 0x2e4e(%rip), %rsi      # 0x1c9b3
callq 0x41c0
jmp 0x19b6c
movq -0x50(%rbp), %rdi
movq 0xf479(%rip), %rsi      # 0x28ff0
movq 0xf43a(%rip), %rdx      # 0x28fb8
callq 0x46a0
jmp 0x19ea3
movq -0x50(%rbp), %rdi
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x20(%rbp)
movl %eax, -0x24(%rbp)
callq 0x4350
jmp 0x19db2
leaq -0x2c(%rbp), %rdi
xorl %esi, %esi
movl $0x8, %edx
callq 0x4270
leaq 0xf867(%rip), %rax      # 0x29420
movl (%rax), %edi
movl $0x1, %esi
movl $0xd, %edx
leaq -0x2c(%rbp), %rcx
movl $0x8, %r8d
callq 0x46f0
cmpl $-0x1, %eax
jne 0x19c31
movl $0x10, %edi
callq 0x4250
movq %rax, %rdi
movq %rdi, %rax
movq %rax, -0x58(%rbp)
leaq 0x2dd7(%rip), %rsi      # 0x1c9cb
callq 0x41c0
jmp 0x19bfb
movq -0x58(%rbp), %rdi
movq 0xf3ea(%rip), %rsi      # 0x28ff0
movq 0xf3ab(%rip), %rdx      # 0x28fb8
callq 0x46a0
jmp 0x19ea3
movq -0x58(%rbp), %rdi
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x20(%rbp)
movl %eax, -0x24(%rbp)
callq 0x4350
jmp 0x19db2
movl $0x1, -0x30(%rbp)
leaq 0xf7e1(%rip), %rax      # 0x29420
movl (%rax), %edi
movl $0x1, %esi
movl $0x2, %edx
leaq -0x30(%rbp), %rcx
movl $0x4, %r8d
callq 0x46f0
cmpl $-0x1, %eax
jne 0x19cb7
movl $0x10, %edi
callq 0x4250
movq %rax, %rdi
movq %rdi, %rax
movq %rax, -0x60(%rbp)
leaq 0x2d68(%rip), %rsi      # 0x1c9e2
callq 0x41c0
jmp 0x19c81
movq -0x60(%rbp), %rdi
movq 0xf364(%rip), %rsi      # 0x28ff0
movq 0xf325(%rip), %rdx      # 0x28fb8
callq 0x46a0
jmp 0x19ea3
movq -0x60(%rbp), %rdi
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x20(%rbp)
movl %eax, -0x24(%rbp)
callq 0x4350
jmp 0x19db2
movw $0x2, -0x14(%rbp)
movl $0xfa0, %edi            # imm = 0xFA0
callq 0x4080
movw %ax, -0x12(%rbp)
movl $0x0, -0x10(%rbp)
leaq 0xf747(%rip), %rax      # 0x29420
movl (%rax), %edi
leaq -0x14(%rbp), %rsi
movl $0x10, %edx
callq 0x40b0
cmpl $-0x1, %eax
jne 0x19d43
movl $0x10, %edi
callq 0x4250
movq %rax, %rdi
movq %rdi, %rax
movq %rax, -0x68(%rbp)
leaq 0x2cf3(%rip), %rsi      # 0x1c9fc
callq 0x41c0
jmp 0x19d10
movq -0x68(%rbp), %rdi
movq 0xf2d5(%rip), %rsi      # 0x28ff0
movq 0xf296(%rip), %rdx      # 0x28fb8
callq 0x46a0
jmp 0x19ea3
movq -0x68(%rbp), %rdi
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x20(%rbp)
movl %eax, -0x24(%rbp)
callq 0x4350
jmp 0x19db2
leaq 0xf6d6(%rip), %rax      # 0x29420
movl (%rax), %edi
movl $0x1000, %esi           # imm = 0x1000
callq 0x44f0
cmpl $-0x1, %eax
jne 0x19e1f
movl $0x10, %edi
callq 0x4250
movq %rax, %rdi
movq %rdi, %rax
movq %rax, -0x70(%rbp)
leaq 0x2c87(%rip), %rsi      # 0x1ca01
callq 0x41c0
jmp 0x19d81
movq -0x70(%rbp), %rdi
movq 0xf264(%rip), %rsi      # 0x28ff0
movq 0xf225(%rip), %rdx      # 0x28fb8
callq 0x46a0
jmp 0x19ea3
movq -0x70(%rbp), %rdi
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x20(%rbp)
movl %eax, -0x24(%rbp)
callq 0x4350
movl -0x24(%rbp), %eax
movl $0x1, %ecx
cmpl %ecx, %eax
jne 0x19e92
movq -0x20(%rbp), %rdi
callq 0x41d0
movq %rax, -0x38(%rbp)
movq 0xf212(%rip), %rdi      # 0x28fe8
leaq 0x2c2b(%rip), %rsi      # 0x1ca08
callq 0x4410
movq %rax, -0x78(%rbp)
jmp 0x19de8
movq -0x78(%rbp), %rdi
movq 0xf1a5(%rip), %rsi      # 0x28f98
callq 0x4480
jmp 0x19dfa
movq -0x38(%rbp), %rdi
movq (%rdi), %rax
movq 0x10(%rax), %rax
callq *%rax
movq %rax, %rdi
callq 0x4380
jmp 0x19e11
movl $0x1, -0x4(%rbp)
callq 0x4650
jmp 0x19e86
xorl %eax, %eax
movl %eax, %edi
callq 0x43e0
movq %rax, %rcx
leaq 0xfaf6(%rip), %rax      # 0x29928
movq %rcx, (%rax)
jmp 0x19e4c
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x20(%rbp)
movl %eax, -0x24(%rbp)
callq 0x4650
jmp 0x19e4a
jmp 0x19e92
movl $0x2, %edi
leaq -0x438(%rip), %rsi      # 0x19a20
callq 0x4510
movl $0xf, %edi
leaq -0x449(%rip), %rsi      # 0x19a20
callq 0x4510
movl $0x1, %edi
leaq -0x45a(%rip), %rsi      # 0x19a20
callq 0x4510
movl $0x0, -0x4(%rbp)
movl -0x4(%rbp), %eax
addq $0x80, %rsp
popq %rbp
retq
movq -0x20(%rbp), %rdi
callq 0x46d0
movq %rax, %rdi
callq 0x6ae0
nopw %cs:(%rax,%rax)