// File: /workspace/github_cpp_makefile_star2_O2/tylerkaraszewski[P]Argyle/Server.cpp
// Function: Server::start()
bool Server::start()
{
  if (listen(m_mainSocket, 10))
  {
    m_logger.logError("Listen error(" + Utils::llToString(errno) + "): " + strerror(errno));
    close(m_mainSocket);
    return false;
  }

  initSocketList();

  while (m_shouldQuit == false)
  {
    int n = poll(m_sockets, m_socketCount, 1000);
    if (n == -1)
    {
      if (errno == EINTR)
      {
        // This happens when we try and exit. For a legit exit, m_shouldQuit should be set. For some other signal,
        // we'll just try and continue.
        continue;
      }
      m_logger.logError("Poll error(" + Utils::llToString(errno) + "): " + strerror(errno));
      close(m_mainSocket);
      return false;
    }
    // No socket activity, just start the next loop.
    if (n == 0)
    {
      continue;
    }

    int socketToWatch = -1;
    vector<int> socketsToUnwatch;
    for (int i = 0; i < m_socketCount; ++i)
    {
      // General error check.
      if (m_sockets[i].revents & POLLERR)
      {
        socketsToUnwatch.push_back(m_sockets[i].fd);
        continue;
      }
      if (m_sockets[i].revents & POLLHUP)
      {
        socketsToUnwatch.push_back(m_sockets[i].fd);
        continue;
      }
      if (m_sockets[i].revents & POLLNVAL)
      {
        socketsToUnwatch.push_back(m_sockets[i].fd);
        continue;
      }

      // Special socket.
      if (m_sockets[i].fd == m_mainSocket)
      {
        if (m_sockets[i].revents & POLLIN)
        {
          struct sockaddr_in newAddress;
          socklen_t newAddressSize = sizeof(newAddress);
          int newSocket = accept(m_mainSocket, (struct sockaddr*)&m_address, &newAddressSize);
          if (newSocket != -1)
          {
            socketToWatch = newSocket;
          }
          else
          {
            m_logger.logError("Accept error(" + Utils::llToString(errno) + "): " + strerror(errno));
          }
        }
      }
      else
      {
        bool isFinished = m_connections[m_sockets[i].fd]->process(m_sockets[i].events, m_sockets[i].revents);
        if (isFinished)
        {
          socketsToUnwatch.push_back(m_sockets[i].fd);
        }
      }
    }

    // @TODO: Timeout expiration goes here (or possibly after the 'unwatchSockets' call below).

    // Rebuild our socket list if anything has changed.
    unwatchSockets(socketsToUnwatch);
    watchSocket(socketToWatch);
  }
  return true;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0xc8, %rsp
movq %rdi, %rbx
movl (%rdi), %edi
pushq $0xa
popq %rsi
callq 0x3300
testl %eax, %eax
je 0x3c79
movq 0x58(%rbx), %r14
callq 0x3040
movq %rax, %r15
movslq (%rax), %rsi
leaq 0x88(%rsp), %r12
movq %r12, %rdi
callq 0x8104
leaq 0x545a(%rip), %rsi      # 0x9050
leaq 0x30(%rsp), %rdi
movq %r12, %rdx
callq 0x42fb
leaq 0x5424(%rip), %rdx      # 0x902e
leaq 0x10(%rsp), %rdi
leaq 0x30(%rsp), %rsi
callq 0x42dc
movl (%r15), %edi
callq 0x3110
leaq 0x50(%rsp), %rdi
leaq 0x10(%rsp), %rsi
movq %rax, %rdx
callq 0x42dc
leaq 0x50(%rsp), %rsi
movq %r14, %rdi
callq 0x80f6
leaq 0x50(%rsp), %rdi
callq 0x35d8
leaq 0x10(%rsp), %rdi
callq 0x35d8
leaq 0x30(%rsp), %rdi
callq 0x35d8
leaq 0x88(%rsp), %rdi
callq 0x35d8
movl (%rbx), %edi
callq 0x35a0
xorl %eax, %eax
jmp 0x3e85
movq %rbx, %rdi
callq 0x3fa8
leaq 0x18(%rbx), %r13
leaq 0x48(%rbx), %rax
movq %rax, 0xc0(%rsp)
leaq 0x30(%rsp), %rbp
movq %r13, 0xa8(%rsp)
cmpb $0x0, 0x16(%rbx)
jne 0x3e83
movq 0x8(%rbx), %rdi
movslq 0x10(%rbx), %rsi
movl $0x3e8, %edx            # imm = 0x3E8
callq 0x3440
testl %eax, %eax
je 0x3c9e
cmpl $-0x1, %eax
jne 0x3cd6
callq 0x3040
movslq (%rax), %rsi
cmpq $0x4, %rsi
je 0x3c9e
jmp 0x3e97
andq $0x0, 0x80(%rsp)
xorps %xmm0, %xmm0
movaps %xmm0, 0x70(%rsp)
xorl %r14d, %r14d
xorl %r12d, %r12d
pushq $-0x1
popq %rax
movl %eax, 0x8(%rsp)
movslq 0x10(%rbx), %rax
cmpq %rax, %r12
jge 0x3e5b
movq 0x8(%rbx), %rsi
movzwl 0x6(%rsi,%r14), %eax
addq %r14, %rsi
testb $0x38, %al
je 0x3d25
leaq 0x70(%rsp), %rdi
callq 0x4320
incq %r12
addq $0x8, %r14
jmp 0x3cf4
movl (%rsi), %edi
cmpl (%rbx), %edi
jne 0x3e19
testb $0x1, %al
je 0x3d1c
movl $0x10, 0xc(%rsp)
movq 0xc0(%rsp), %rsi
leaq 0xc(%rsp), %rdx
callq 0x30f0
cmpl $-0x1, %eax
jne 0x3e10
movq 0x58(%rbx), %rax
movq %rax, 0xb0(%rsp)
callq 0x3040
movq %rax, 0xb8(%rsp)
movslq (%rax), %rsi
leaq 0x88(%rsp), %r13
movq %r13, %rdi
callq 0x8104
movq %rbp, %rdi
leaq 0x52de(%rip), %rsi      # 0x906a
movq %r13, %rdx
callq 0x42fb
leaq 0x10(%rsp), %r13
movq %r13, %rdi
movq %rbp, %rsi
leaq 0x5288(%rip), %rdx      # 0x902e
callq 0x42dc
movq 0xb8(%rsp), %rax
movl (%rax), %edi
callq 0x3110
leaq 0x50(%rsp), %r15
movq %r15, %rdi
movq %r13, %rsi
movq %rax, %rdx
callq 0x42dc
movq 0xb0(%rsp), %rdi
movq %r15, %rsi
callq 0x80f6
movq 0xa8(%rsp), %r13
movq %r15, %rdi
callq 0x35d8
leaq 0x10(%rsp), %rdi
callq 0x35d8
movq %rbp, %rdi
callq 0x35d8
leaq 0x88(%rsp), %rdi
callq 0x35d8
movl 0x8(%rsp), %eax
movl %eax, 0x8(%rsp)
jmp 0x3d1c
movq %r13, %rdi
callq 0x433e
movq (%rax), %rdi
movq 0x8(%rbx), %rax
leaq (%rax,%r14), %rsi
addq $0x4, %rsi
leaq (%rax,%r14), %rdx
addq $0x6, %rdx
callq 0x4a64
testb %al, %al
je 0x3d1c
movq 0x8(%rbx), %rsi
addq %r14, %rsi
leaq 0x70(%rsp), %rdi
callq 0x4320
jmp 0x3d1c
movq %rbx, %rdi
leaq 0x70(%rsp), %rsi
callq 0x403a
movq %rbx, %rdi
movl 0x8(%rsp), %esi
callq 0x4158
leaq 0x70(%rsp), %rdi
callq 0x442a
jmp 0x3c9e
movb $0x1, %al
addq $0xc8, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movq %rax, %r12
movq 0x58(%rbx), %r14
leaq 0x88(%rsp), %r15
movq %r15, %rdi
callq 0x8104
leaq 0x51a9(%rip), %rsi      # 0x905e
leaq 0x30(%rsp), %rdi
movq %r15, %rdx
callq 0x42fb
leaq 0x5165(%rip), %rdx      # 0x902e
leaq 0x10(%rsp), %rdi
leaq 0x30(%rsp), %rsi
callq 0x42dc
movl (%r12), %edi
callq 0x3110
leaq 0x50(%rsp), %rdi
leaq 0x10(%rsp), %rsi
movq %rax, %rdx
callq 0x42dc
leaq 0x50(%rsp), %rsi
movq %r14, %rdi
callq 0x80f6
jmp 0x3c40
jmp 0x3f0d
jmp 0x3f1c
jmp 0x3f2b
jmp 0x3f3a
movq %rax, %rbx
leaq 0x50(%rsp), %rdi
callq 0x35d8
jmp 0x3f1f
movq %rax, %rbx
leaq 0x10(%rsp), %rdi
callq 0x35d8
jmp 0x3f2e
movq %rax, %rbx
leaq 0x30(%rsp), %rdi
callq 0x35d8
jmp 0x3f3d
movq %rax, %rbx
leaq 0x88(%rsp), %rdi
callq 0x35d8
jmp 0x3fa0
movq %rax, %rbx
jmp 0x3f7e
movq %rax, %rbx
jmp 0x3f6a
jmp 0x3f93
movq %rax, %rbx
jmp 0x3f74
movq %rax, %rbx
leaq 0x50(%rsp), %rdi
callq 0x35d8
leaq 0x10(%rsp), %rdi
callq 0x35d8
leaq 0x30(%rsp), %rdi
callq 0x35d8
leaq 0x88(%rsp), %rdi
callq 0x35d8
jmp 0x3f96
jmp 0x3f93
jmp 0x3f93
jmp 0x3f93
movq %rax, %rbx
leaq 0x70(%rsp), %rdi
callq 0x442a
movq %rbx, %rdi
callq 0x3510