// File: 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 %rbp
movq %rsp, %rbp
subq $0x220, %rsp            # imm = 0x220
movq %rdi, -0x8(%rbp)
movq -0x8(%rbp), %rax
movq %rax, -0x180(%rbp)
callq 0x3a90
movq %rax, %rdi
callq 0xaf40
callq 0x3a90
movq %rax, %rdi
callq 0x9b30
movl $0x2, %edi
movl $0x1, %esi
movl $0x6, %edx
callq 0x30c0
movq -0x180(%rbp), %rcx
movl %eax, (%rcx)
cmpl $0x0, %eax
jge 0x5fff
callq 0x43c0
movq %rax, %rdi
leaq 0x61c2(%rip), %rsi      # 0xc10b
callq 0x5520
movq %rax, %rdi
leaq 0x61bd(%rip), %rsi      # 0xc115
movq %rsi, -0x190(%rbp)
callq 0x5520
movq %rax, %rdi
leaq 0x65ec(%rip), %rsi      # 0xc55a
callq 0x5520
callq 0x3a90
movq %rax, -0x198(%rbp)
leaq -0x29(%rbp), %rdi
movq %rdi, -0x188(%rbp)
callq 0x35e0
movq -0x190(%rbp), %rsi
movq -0x188(%rbp), %rdx
leaq -0x28(%rbp), %rdi
callq 0x42f0
jmp 0x5fa8
movq -0x198(%rbp), %rdi
leaq -0x28(%rbp), %rsi
callq 0xaf80
jmp 0x5fba
leaq -0x28(%rbp), %rdi
callq 0x3200
leaq -0x29(%rbp), %rdi
callq 0x3400
jmp 0x5fff
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x38(%rbp)
movl %eax, -0x3c(%rbp)
jmp 0x5ff1
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x38(%rbp)
movl %eax, -0x3c(%rbp)
leaq -0x28(%rbp), %rdi
callq 0x3200
leaq -0x29(%rbp), %rdi
callq 0x3400
jmp 0x66ed
leaq -0x4c(%rbp), %rdi
xorl %esi, %esi
movl $0x10, %edx
callq 0x31e0
movw $0x2, -0x4c(%rbp)
xorl %edi, %edi
callq 0x32b0
movl %eax, -0x48(%rbp)
movl $0x1e61, %edi           # imm = 0x1E61
callq 0x3050
movw %ax, %cx
movq -0x180(%rbp), %rax
movw %cx, -0x4a(%rbp)
movl (%rax), %edi
leaq -0x4c(%rbp), %rsi
movl $0x10, %edx
callq 0x3060
cmpl $0x0, %eax
jge 0x610e
callq 0x43c0
movq %rax, %rdi
leaq 0x60ac(%rip), %rsi      # 0xc10b
callq 0x5520
movq %rax, %rdi
leaq 0x60be(%rip), %rsi      # 0xc12c
callq 0x5520
movq %rax, %rdi
leaq 0x64dd(%rip), %rsi      # 0xc55a
callq 0x5520
callq 0x3a90
movq %rax, -0x1a8(%rbp)
leaq -0x71(%rbp), %rdi
movq %rdi, -0x1a0(%rbp)
callq 0x35e0
movq -0x1a0(%rbp), %rdx
leaq 0x608e(%rip), %rsi      # 0xc13a
leaq -0x70(%rbp), %rdi
callq 0x42f0
jmp 0x60b7
movq -0x1a8(%rbp), %rdi
leaq -0x70(%rbp), %rsi
callq 0xaf80
jmp 0x60c9
leaq -0x70(%rbp), %rdi
callq 0x3200
leaq -0x71(%rbp), %rdi
callq 0x3400
jmp 0x610e
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x38(%rbp)
movl %eax, -0x3c(%rbp)
jmp 0x6100
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x38(%rbp)
movl %eax, -0x3c(%rbp)
leaq -0x70(%rbp), %rdi
callq 0x3200
leaq -0x71(%rbp), %rdi
callq 0x3400
jmp 0x66ed
movq -0x180(%rbp), %rax
movl $0x10, -0x88(%rbp)
movl (%rax), %edi
leaq -0x84(%rbp), %rsi
leaq -0x88(%rbp), %rdx
callq 0x3330
cmpl $0x0, %eax
jge 0x6210
callq 0x43c0
movq %rax, %rdi
leaq 0x5fbf(%rip), %rsi      # 0xc10b
callq 0x5520
movq %rax, %rdi
leaq 0x5ff3(%rip), %rsi      # 0xc14e
callq 0x5520
movq %rax, %rdi
leaq 0x63f0(%rip), %rsi      # 0xc55a
callq 0x5520
callq 0x3a90
movq %rax, -0x1b8(%rbp)
leaq -0xa9(%rbp), %rdi
movq %rdi, -0x1b0(%rbp)
callq 0x35e0
movq -0x1b0(%rbp), %rdx
leaq 0x5fd4(%rip), %rsi      # 0xc170
leaq -0xa8(%rbp), %rdi
callq 0x42f0
jmp 0x61aa
movq -0x1b8(%rbp), %rdi
leaq -0xa8(%rbp), %rsi
callq 0xaf80
jmp 0x61bf
leaq -0xa8(%rbp), %rdi
callq 0x3200
leaq -0xa9(%rbp), %rdi
callq 0x3400
jmp 0x6210
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x38(%rbp)
movl %eax, -0x3c(%rbp)
jmp 0x61ff
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x38(%rbp)
movl %eax, -0x3c(%rbp)
leaq -0xa8(%rbp), %rdi
callq 0x3200
leaq -0xa9(%rbp), %rdi
callq 0x3400
jmp 0x66ed
callq 0x43c0
movq %rax, %rdi
leaq 0x5f72(%rip), %rsi      # 0xc191
callq 0x5520
movq %rax, %rdi
leaq 0x5f6c(%rip), %rsi      # 0xc19a
callq 0x5520
movq %rax, -0x1d0(%rbp)
movl -0x80(%rbp), %eax
movl %eax, -0xb0(%rbp)
movl -0xb0(%rbp), %edi
callq 0x31b0
movq -0x1d0(%rbp), %rdi
movq %rax, %rsi
callq 0x5520
movq %rax, %rdi
leaq 0x5f47(%rip), %rsi      # 0xc1ae
callq 0x5520
movq %rax, %rdi
leaq 0x62e4(%rip), %rsi      # 0xc55a
callq 0x5520
callq 0x3a90
movq %rax, -0x1c8(%rbp)
leaq -0xd1(%rbp), %rdi
movq %rdi, -0x1c0(%rbp)
callq 0x35e0
movq -0x1c0(%rbp), %rdx
leaq 0x5f0c(%rip), %rsi      # 0xc1b4
leaq -0xd0(%rbp), %rdi
callq 0x42f0
jmp 0x62b6
movq -0x1c8(%rbp), %rdi
leaq -0xd0(%rbp), %rsi
movl $0x5, %edx
callq 0xa260
jmp 0x62d0
leaq -0xd0(%rbp), %rdi
callq 0x3200
leaq -0xd1(%rbp), %rdi
callq 0x3400
callq 0x3a90
movq %rax, %rdi
callq 0x9ce0
movq -0x180(%rbp), %rax
movl (%rax), %edi
movl $0x3, %esi
callq 0x3430
cmpl $0x0, %eax
jge 0x6422
callq 0x43c0
movq %rax, %rdi
leaq 0x5deb(%rip), %rsi      # 0xc10b
callq 0x5520
movq %rax, %rdi
leaq 0x5ea1(%rip), %rsi      # 0xc1d0
movq %rsi, -0x1e0(%rbp)
callq 0x5520
movq %rax, %rdi
leaq 0x6215(%rip), %rsi      # 0xc55a
callq 0x5520
callq 0x3a90
movq %rax, -0x1e8(%rbp)
leaq -0xf9(%rbp), %rdi
movq %rdi, -0x1d8(%rbp)
callq 0x35e0
movq -0x1e0(%rbp), %rsi
movq -0x1d8(%rbp), %rdx
leaq -0xf8(%rbp), %rdi
callq 0x42f0
jmp 0x6385
movq -0x1e8(%rbp), %rdi
leaq -0xf8(%rbp), %rsi
callq 0xaf80
jmp 0x639a
leaq -0xf8(%rbp), %rdi
callq 0x3200
leaq -0xf9(%rbp), %rdi
callq 0x3400
jmp 0x6422
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x38(%rbp)
movl %eax, -0x3c(%rbp)
jmp 0x63da
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x38(%rbp)
movl %eax, -0x3c(%rbp)
leaq -0xd0(%rbp), %rdi
callq 0x3200
leaq -0xd1(%rbp), %rdi
callq 0x3400
jmp 0x66ed
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x38(%rbp)
movl %eax, -0x3c(%rbp)
jmp 0x6411
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x38(%rbp)
movl %eax, -0x3c(%rbp)
leaq -0xf8(%rbp), %rdi
callq 0x3200
leaq -0xf9(%rbp), %rdi
callq 0x3400
jmp 0x66ed
callq 0x43c0
movq %rax, %rdi
leaq 0x5d60(%rip), %rsi      # 0xc191
callq 0x5520
movq %rax, %rdi
leaq 0x5dad(%rip), %rsi      # 0xc1ed
callq 0x5520
movq %rax, %rdi
leaq 0x610b(%rip), %rsi      # 0xc55a
callq 0x5520
callq 0x3a90
movq %rax, -0x1f8(%rbp)
leaq -0x121(%rbp), %rdi
movq %rdi, -0x1f0(%rbp)
callq 0x35e0
movq -0x1f0(%rbp), %rdx
leaq 0x5d8b(%rip), %rsi      # 0xc20c
leaq -0x120(%rbp), %rdi
callq 0x42f0
jmp 0x648f
movq -0x1f8(%rbp), %rdi
leaq -0x120(%rbp), %rsi
movl $0x6, %edx
callq 0xa260
jmp 0x64a9
leaq -0x120(%rbp), %rdi
callq 0x3200
leaq -0x121(%rbp), %rdi
callq 0x3400
callq 0x3a90
movq %rax, %rdi
callq 0x9ce0
movq -0x180(%rbp), %rax
movl (%rax), %edi
xorl %eax, %eax
movl %eax, %edx
movq %rdx, %rsi
callq 0x3110
movl %eax, -0x128(%rbp)
cmpl $0x0, %eax
jge 0x65fc
callq 0x43c0
movq %rax, %rdi
leaq 0x5c0a(%rip), %rsi      # 0xc10b
callq 0x5520
movq %rax, %rdi
leaq 0x5d1a(%rip), %rsi      # 0xc22a
callq 0x5520
movq %rax, %rdi
leaq 0x603b(%rip), %rsi      # 0xc55a
callq 0x5520
callq 0x3a90
movq %rax, -0x208(%rbp)
leaq -0x149(%rbp), %rdi
movq %rdi, -0x200(%rbp)
callq 0x35e0
movq -0x200(%rbp), %rdx
leaq 0x5ce8(%rip), %rsi      # 0xc239
leaq -0x148(%rbp), %rdi
callq 0x42f0
jmp 0x655f
movq -0x208(%rbp), %rdi
leaq -0x148(%rbp), %rsi
callq 0xaf80
jmp 0x6574
leaq -0x148(%rbp), %rdi
callq 0x3200
leaq -0x149(%rbp), %rdi
callq 0x3400
jmp 0x65fc
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x38(%rbp)
movl %eax, -0x3c(%rbp)
jmp 0x65b4
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x38(%rbp)
movl %eax, -0x3c(%rbp)
leaq -0x120(%rbp), %rdi
callq 0x3200
leaq -0x121(%rbp), %rdi
callq 0x3400
jmp 0x66ed
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x38(%rbp)
movl %eax, -0x3c(%rbp)
jmp 0x65eb
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x38(%rbp)
movl %eax, -0x3c(%rbp)
leaq -0x148(%rbp), %rdi
callq 0x3200
leaq -0x149(%rbp), %rdi
callq 0x3400
jmp 0x66ed
callq 0x43c0
movq %rax, %rdi
leaq 0x5b86(%rip), %rsi      # 0xc191
callq 0x5520
movq %rax, %rdi
leaq 0x5c2d(%rip), %rsi      # 0xc247
movq %rsi, -0x218(%rbp)
callq 0x5520
movq %rax, %rdi
leaq 0x5f2a(%rip), %rsi      # 0xc55a
callq 0x5520
callq 0x3a90
movq %rax, -0x220(%rbp)
leaq -0x171(%rbp), %rdi
movq %rdi, -0x210(%rbp)
callq 0x35e0
movq -0x218(%rbp), %rsi
movq -0x210(%rbp), %rdx
leaq -0x170(%rbp), %rdi
callq 0x42f0
jmp 0x6670
movq -0x220(%rbp), %rdi
leaq -0x170(%rbp), %rsi
movl $0x7, %edx
callq 0xa260
jmp 0x668a
leaq -0x170(%rbp), %rdi
callq 0x3200
leaq -0x171(%rbp), %rdi
callq 0x3400
movl $0x1, %edi
callq 0x32a0
movl -0x128(%rbp), %eax
addq $0x220, %rsp            # imm = 0x220
popq %rbp
retq
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x38(%rbp)
movl %eax, -0x3c(%rbp)
jmp 0x66e1
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x38(%rbp)
movl %eax, -0x3c(%rbp)
leaq -0x170(%rbp), %rdi
callq 0x3200
leaq -0x171(%rbp), %rdi
callq 0x3400
movq -0x38(%rbp), %rdi
callq 0x35d0
nopw %cs:(%rax,%rax)