// File: /workspace/github_cpp_makefile_star2_O3/syky27[P]TicTacToe/src/DNNetworkConnection.cpp
// Function: DNNetworkConnection::startServer()
int DNNetworkConnection::startServer()
{
    DNWindow::getInstance().clearScreen();
    DNWindow::getInstance().drawHeader();
    
    

    /* create socket */
    if ((sockDesc = socket(PF_INET, SOCK_STREAM, IPPROTO_TCP)) < 0) {
        DNLogService::sharedObject() << "ERROR => " << "Can not create socket!" << "\n";
        DNWindow::getInstance().terminateWithErrorMessage(string("Can not create socket!"));
    }

    /* bind socket to any addr and port 7777 */
    sockaddr_in localAddr;
    memset(& localAddr, 0, sizeof ( localAddr)); //clear it from garbage
    localAddr.sin_family = AF_INET;
    localAddr.sin_addr.s_addr = htonl(INADDR_ANY);
    localAddr.sin_port = htons(7777);

    if (bind(sockDesc, (sockaddr *) & localAddr, sizeof ( sockaddr_in)) < 0) {
        DNLogService::sharedObject() << "ERROR => " << "Can not bind!" << "\n";
        DNWindow::getInstance().terminateWithErrorMessage(string("Cannot bind to port"));
       
    }

    /* get local address */
    sockaddr_in addr;
    unsigned int addr_len = sizeof (addr);
    if (getsockname(sockDesc, (sockaddr *) & addr, (socklen_t *) & addr_len) < 0) {
        DNLogService::sharedObject() << "ERROR => " << "Unable to fetch local ip address!" << "\n";
        DNWindow::getInstance().terminateWithErrorMessage(string("Unable to fetch local IP address"));
    }
    DNLogService::sharedObject() << "INFO => " << "Started server on: " << inet_ntoa(addr.sin_addr) << ":7777" << "\n";
    DNWindow::getInstance().middleString(string("Server started on port 7777"), 5);
    DNWindow::getInstance().refreshDN();
    

    /* start listening */
    if (listen(sockDesc, 3) < 0) //3 is just in case.. one never know..
    {
        DNLogService::sharedObject() << "ERROR => " << "Set listening socket failed!" << "\n";
        DNWindow::getInstance().terminateWithErrorMessage(string("Set listening socket failed!"));
    }
    
    DNLogService::sharedObject() << "INFO => " << "Waiting for client to connect." << "\n";
    DNWindow::getInstance().middleString(string("Waiting for client to connect"), 6);
    DNWindow::getInstance().refreshDN();
    /* accept client */
    int clientSockDesc;
    if ((clientSockDesc = accept(sockDesc, NULL, 0)) < 0) {
        DNLogService::sharedObject() << "ERROR => " << "Accept failed!" << "\n";
        DNWindow::getInstance().terminateWithErrorMessage(string("Accept failed"));
                
    }

    DNLogService::sharedObject() << "INFO => " << "Client connected, starting game" << "\n";
    DNWindow::getInstance().middleString(string("Client connected, starting game"), 7);
    sleep(1);

    return clientSockDesc;


    //Game l_game (PLAYER_VS_PLAYER_NETWORK, clientSockDesc);
    //   createServerGame ( clientSockDesc );    
}

// Assembly:
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x130, %rsp            # imm = 0x130
movq %rdi, %rbx
callq 0x39ee
movq %rax, %rdi
callq 0x8296
callq 0x39ee
movq %rax, %rdi
callq 0x7692
movl $0x2, %edi
movl $0x1, %esi
movl $0x6, %edx
callq 0x30a0
movl %eax, (%rbx)
testl %eax, %eax
jns 0x550e
callq 0x3f98
leaq 0x3c20(%rip), %rsi      # 0x90af
movq %rax, %rdi
callq 0x4ca4
leaq 0x3c1b(%rip), %r14      # 0x90b9
movq %rax, %rdi
movq %r14, %rsi
callq 0x4ca4
leaq 0x405e(%rip), %rsi      # 0x950e
movq %rax, %rdi
callq 0x4ca4
callq 0x39ee
movq %rax, %r15
leaq 0x100(%rsp), %r13
movq %r13, -0x10(%r13)
leaq 0x3bfc(%rip), %rdx      # 0x90cf
leaq 0xf0(%rsp), %r12
movq %r12, %rdi
movq %r14, %rsi
callq 0x404c
movq %r15, %rdi
movq %r12, %rsi
callq 0x82b0
movq 0xf0(%rsp), %rdi
cmpq %r13, %rdi
je 0x550e
movq 0x100(%rsp), %rsi
incq %rsi
callq 0x32f0
leaq 0x120(%rsp), %rsi
movq $0x0, 0x8(%rsi)
movq $0x611e0002, (%rsi)     # imm = 0x611E0002
movl (%rbx), %edi
movl $0x10, %edx
callq 0x3050
testl %eax, %eax
jns 0x55c7
callq 0x3f98
leaq 0x3b6a(%rip), %rsi      # 0x90af
movq %rax, %rdi
callq 0x4ca4
leaq 0x3b7c(%rip), %rsi      # 0x90d0
movq %rax, %rdi
callq 0x4ca4
leaq 0x3fab(%rip), %rsi      # 0x950e
movq %rax, %rdi
callq 0x4ca4
callq 0x39ee
movq %rax, %r14
leaq 0xe0(%rsp), %r15
movq %r15, -0x10(%r15)
leaq 0x3b58(%rip), %rsi      # 0x90de
leaq 0x3b64(%rip), %rdx      # 0x90f1
leaq 0xd0(%rsp), %rdi
callq 0x404c
leaq 0xd0(%rsp), %rsi
movq %r14, %rdi
callq 0x82b0
movq 0xd0(%rsp), %rdi
cmpq %r15, %rdi
je 0x55c7
movq 0xe0(%rsp), %rsi
incq %rsi
callq 0x32f0
leaq 0xc(%rsp), %rdx
movl $0x10, (%rdx)
movl (%rbx), %edi
leaq 0x110(%rsp), %rsi
callq 0x32a0
testl %eax, %eax
jns 0x5677
callq 0x3f98
leaq 0x3aba(%rip), %rsi      # 0x90af
movq %rax, %rdi
callq 0x4ca4
leaq 0x3aee(%rip), %rsi      # 0x90f2
movq %rax, %rdi
callq 0x4ca4
leaq 0x3efb(%rip), %rsi      # 0x950e
movq %rax, %rdi
callq 0x4ca4
callq 0x39ee
movq %rax, %r14
leaq 0xc0(%rsp), %r15
movq %r15, -0x10(%r15)
leaq 0x3ade(%rip), %rsi      # 0x9114
leaq 0x3af7(%rip), %rdx      # 0x9134
leaq 0xb0(%rsp), %rdi
callq 0x404c
leaq 0xb0(%rsp), %rsi
movq %r14, %rdi
callq 0x82b0
movq 0xb0(%rsp), %rdi
cmpq %r15, %rdi
je 0x5677
movq 0xc0(%rsp), %rsi
incq %rsi
callq 0x32f0
callq 0x3f98
leaq 0x3ab2(%rip), %rsi      # 0x9135
movq %rax, %rdi
callq 0x4ca4
leaq 0x3aac(%rip), %rsi      # 0x913e
movq %rax, %rdi
callq 0x4ca4
movq %rax, %r14
movl 0x114(%rsp), %edi
callq 0x3180
movq %r14, %rdi
movq %rax, %rsi
callq 0x4ca4
leaq 0x3a97(%rip), %rsi      # 0x9152
movq %rax, %rdi
callq 0x4ca4
leaq 0x3e44(%rip), %rsi      # 0x950e
movq %rax, %rdi
callq 0x4ca4
callq 0x39ee
movq %rax, %r14
leaq 0xa0(%rsp), %r15
movq %r15, -0x10(%r15)
leaq 0x3a6b(%rip), %rsi      # 0x9158
leaq 0x3a7f(%rip), %rdx      # 0x9173
leaq 0x90(%rsp), %rdi
callq 0x404c
leaq 0x90(%rsp), %rsi
movq %r14, %rdi
movl $0x5, %edx
callq 0x7b46
movq 0x90(%rsp), %rdi
cmpq %r15, %rdi
je 0x5733
movq 0xa0(%rsp), %rsi
incq %rsi
callq 0x32f0
callq 0x39ee
movq %rax, %rdi
callq 0x7794
movl (%rbx), %edi
movl $0x3, %esi
callq 0x3340
testl %eax, %eax
jns 0x57d9
callq 0x3f98
leaq 0x394f(%rip), %rsi      # 0x90af
movq %rax, %rdi
callq 0x4ca4
leaq 0x3a05(%rip), %r15      # 0x9174
movq %rax, %rdi
movq %r15, %rsi
callq 0x4ca4
leaq 0x3d8d(%rip), %rsi      # 0x950e
movq %rax, %rdi
callq 0x4ca4
callq 0x39ee
movq %rax, %r14
leaq 0x80(%rsp), %r12
movq %r12, -0x10(%r12)
leaq 0x39eb(%rip), %rdx      # 0x9190
leaq 0x70(%rsp), %rdi
movq %r15, %rsi
callq 0x404c
leaq 0x70(%rsp), %rsi
movq %r14, %rdi
callq 0x82b0
movq 0x70(%rsp), %rdi
cmpq %r12, %rdi
je 0x57d9
movq 0x80(%rsp), %rsi
incq %rsi
callq 0x32f0
callq 0x3f98
leaq 0x3950(%rip), %rsi      # 0x9135
movq %rax, %rdi
callq 0x4ca4
leaq 0x399d(%rip), %rsi      # 0x9191
movq %rax, %rdi
callq 0x4ca4
leaq 0x3d0b(%rip), %rsi      # 0x950e
movq %rax, %rdi
callq 0x4ca4
callq 0x39ee
movq %rax, %r14
leaq 0x60(%rsp), %r15
movq %r15, -0x10(%r15)
leaq 0x398d(%rip), %rsi      # 0x91b0
leaq 0x39a3(%rip), %rdx      # 0x91cd
leaq 0x50(%rsp), %rdi
callq 0x404c
leaq 0x50(%rsp), %rsi
movq %r14, %rdi
movl $0x6, %edx
callq 0x7b46
movq 0x50(%rsp), %rdi
cmpq %r15, %rdi
je 0x585d
movq 0x60(%rsp), %rsi
incq %rsi
callq 0x32f0
callq 0x39ee
movq %rax, %rdi
callq 0x7794
movl (%rbx), %edi
xorl %esi, %esi
xorl %edx, %edx
callq 0x3100
movl %eax, %ebx
testl %eax, %eax
jns 0x58fa
callq 0x3f98
leaq 0x3828(%rip), %rsi      # 0x90af
movq %rax, %rdi
callq 0x4ca4
leaq 0x3938(%rip), %rsi      # 0x91ce
movq %rax, %rdi
callq 0x4ca4
leaq 0x3c69(%rip), %rsi      # 0x950e
movq %rax, %rdi
callq 0x4ca4
callq 0x39ee
movq %rax, %r14
leaq 0x40(%rsp), %r15
movq %r15, -0x10(%r15)
leaq 0x3918(%rip), %rsi      # 0x91dd
leaq 0x391e(%rip), %rdx      # 0x91ea
leaq 0x30(%rsp), %rdi
callq 0x404c
leaq 0x30(%rsp), %rsi
movq %r14, %rdi
callq 0x82b0
movq 0x30(%rsp), %rdi
cmpq %r15, %rdi
je 0x58fa
movq 0x40(%rsp), %rsi
incq %rsi
callq 0x32f0
callq 0x3f98
leaq 0x382f(%rip), %rsi      # 0x9135
movq %rax, %rdi
callq 0x4ca4
leaq 0x38d6(%rip), %r15      # 0x91eb
movq %rax, %rdi
movq %r15, %rsi
callq 0x4ca4
leaq 0x3be7(%rip), %rsi      # 0x950e
movq %rax, %rdi
callq 0x4ca4
callq 0x39ee
movq %rax, %r14
leaq 0x20(%rsp), %r12
movq %r12, -0x10(%r12)
leaq 0x38c2(%rip), %rdx      # 0x920a
leaq 0x10(%rsp), %rdi
movq %r15, %rsi
callq 0x404c
leaq 0x10(%rsp), %rsi
movq %r14, %rdi
movl $0x7, %edx
callq 0x7b46
movq 0x10(%rsp), %rdi
cmpq %r12, %rdi
je 0x597e
movq 0x20(%rsp), %rsi
incq %rsi
callq 0x32f0
movl $0x1, %edi
callq 0x3230
movl %ebx, %eax
addq $0x130, %rsp            # imm = 0x130
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
retq
movq %rax, %rbx
movq 0x30(%rsp), %rdi
cmpq %r15, %rdi
je 0x5a85
movq 0x40(%rsp), %rsi
jmp 0x5a78
jmp 0x5a82
movq %rax, %rbx
movq 0x70(%rsp), %rdi
cmpq %r12, %rdi
je 0x5a85
movq 0x80(%rsp), %rsi
jmp 0x5a78
jmp 0x5a82
movq %rax, %rbx
movq 0xb0(%rsp), %rdi
cmpq %r15, %rdi
je 0x5a85
movq 0xc0(%rsp), %rsi
jmp 0x5a78
movq %rax, %rbx
movq 0xd0(%rsp), %rdi
cmpq %r15, %rdi
je 0x5a85
movq 0xe0(%rsp), %rsi
jmp 0x5a78
jmp 0x5a82
jmp 0x5a82
movq %rax, %rbx
movq 0xf0(%rsp), %rdi
cmpq %r13, %rdi
je 0x5a85
movq 0x100(%rsp), %rsi
jmp 0x5a78
movq %rax, %rbx
movq 0x10(%rsp), %rdi
cmpq %r12, %rdi
je 0x5a85
movq 0x20(%rsp), %rsi
jmp 0x5a78
jmp 0x5a82
movq %rax, %rbx
movq 0x50(%rsp), %rdi
cmpq %r15, %rdi
je 0x5a85
movq 0x60(%rsp), %rsi
jmp 0x5a78
jmp 0x5a82
movq %rax, %rbx
movq 0x90(%rsp), %rdi
cmpq %r15, %rdi
je 0x5a85
movq 0xa0(%rsp), %rsi
incq %rsi
callq 0x32f0
jmp 0x5a85
movq %rax, %rbx
movq %rbx, %rdi
callq 0x34b0
nop