// File: /workspace/github_cpp_makefile_star2_O2/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 %rbx
subq $0x130, %rsp            # imm = 0x130
movq %rdi, %rbx
callq 0x381e
movq %rax, %rdi
callq 0x7046
callq 0x381e
movq %rax, %rdi
callq 0x657a
pushq $0x2
popq %rdi
pushq $0x1
popq %rsi
pushq $0x6
popq %rdx
callq 0x3090
movl %eax, (%rbx)
testl %eax, %eax
jns 0x4bad
callq 0x3c5a
leaq 0x356a(%rip), %rsi      # 0x80af
movq %rax, %rdi
callq 0x4576
leaq 0x3565(%rip), %r15      # 0x80b9
movq %rax, %rdi
movq %r15, %rsi
callq 0x4576
leaq 0x39a8(%rip), %rsi      # 0x850e
movq %rax, %rdi
callq 0x4576
callq 0x381e
movq %rax, %r14
leaq 0x100(%rsp), %rdi
leaq 0x120(%rsp), %rdx
movq %r15, %rsi
callq 0x3c1e
leaq 0x100(%rsp), %rsi
movq %r14, %rdi
callq 0x7060
leaq 0x100(%rsp), %rdi
callq 0x3170
movl (%rbx), %eax
leaq 0x120(%rsp), %rsi
andq $0x0, 0x8(%rsi)
movq $0x611e0002, (%rsi)     # imm = 0x611E0002
pushq $0x10
popq %rdx
movl %eax, %edi
callq 0x3040
testl %eax, %eax
jns 0x4c3f
callq 0x3c5a
leaq 0x34d4(%rip), %rsi      # 0x80af
movq %rax, %rdi
callq 0x4576
leaq 0x34e6(%rip), %rsi      # 0x80d0
movq %rax, %rdi
callq 0x4576
leaq 0x3915(%rip), %rsi      # 0x850e
movq %rax, %rdi
callq 0x4576
callq 0x381e
movq %rax, %r14
leaq 0x34ce(%rip), %rsi      # 0x80de
leaq 0xe0(%rsp), %rdi
leaq 0x10(%rsp), %rdx
callq 0x3c1e
leaq 0xe0(%rsp), %rsi
movq %r14, %rdi
callq 0x7060
leaq 0xe0(%rsp), %rdi
callq 0x3170
leaq 0xc(%rsp), %rdx
movl $0x10, (%rdx)
movl (%rbx), %edi
leaq 0x10(%rsp), %rsi
callq 0x32a0
testl %eax, %eax
jns 0x4cca
callq 0x3c5a
leaq 0x3449(%rip), %rsi      # 0x80af
movq %rax, %rdi
callq 0x4576
leaq 0x347d(%rip), %rsi      # 0x80f2
movq %rax, %rdi
callq 0x4576
leaq 0x388a(%rip), %rsi      # 0x850e
movq %rax, %rdi
callq 0x4576
callq 0x381e
movq %rax, %r14
leaq 0x3479(%rip), %rsi      # 0x8114
leaq 0xc0(%rsp), %rdi
leaq 0xb(%rsp), %rdx
callq 0x3c1e
leaq 0xc0(%rsp), %rsi
movq %r14, %rdi
callq 0x7060
leaq 0xc0(%rsp), %rdi
callq 0x3170
callq 0x3c5a
leaq 0x345f(%rip), %rsi      # 0x8135
movq %rax, %rdi
callq 0x4576
leaq 0x3459(%rip), %rsi      # 0x813e
movq %rax, %rdi
callq 0x4576
movq %rax, %r14
movl 0x14(%rsp), %edi
callq 0x3160
movq %r14, %rdi
movq %rax, %rsi
callq 0x4576
leaq 0x3447(%rip), %rsi      # 0x8152
movq %rax, %rdi
callq 0x4576
leaq 0x37f4(%rip), %rsi      # 0x850e
movq %rax, %rdi
callq 0x4576
callq 0x381e
movq %rax, %r14
leaq 0x3427(%rip), %rsi      # 0x8158
leaq 0xa0(%rsp), %rdi
leaq 0xb(%rsp), %rdx
callq 0x3c1e
leaq 0xa0(%rsp), %rsi
pushq $0x5
popq %rdx
movq %r14, %rdi
callq 0x6a20
leaq 0xa0(%rsp), %rdi
callq 0x3170
callq 0x381e
movq %rax, %rdi
callq 0x665a
movl (%rbx), %edi
pushq $0x3
popq %rsi
callq 0x3340
testl %eax, %eax
jns 0x4ded
callq 0x3c5a
leaq 0x3325(%rip), %rsi      # 0x80af
movq %rax, %rdi
callq 0x4576
leaq 0x33db(%rip), %r15      # 0x8174
movq %rax, %rdi
movq %r15, %rsi
callq 0x4576
leaq 0x3763(%rip), %rsi      # 0x850e
movq %rax, %rdi
callq 0x4576
callq 0x381e
movq %rax, %r14
leaq 0x80(%rsp), %rdi
leaq 0xb(%rsp), %rdx
movq %r15, %rsi
callq 0x3c1e
leaq 0x80(%rsp), %rsi
movq %r14, %rdi
callq 0x7060
leaq 0x80(%rsp), %rdi
callq 0x3170
callq 0x3c5a
leaq 0x333c(%rip), %rsi      # 0x8135
movq %rax, %rdi
callq 0x4576
leaq 0x3389(%rip), %rsi      # 0x8191
movq %rax, %rdi
callq 0x4576
leaq 0x36f7(%rip), %rsi      # 0x850e
movq %rax, %rdi
callq 0x4576
callq 0x381e
movq %rax, %r14
leaq 0x3382(%rip), %rsi      # 0x81b0
leaq 0x60(%rsp), %rdi
leaq 0xb(%rsp), %rdx
callq 0x3c1e
leaq 0x60(%rsp), %rsi
pushq $0x6
popq %rdx
movq %r14, %rdi
callq 0x6a20
leaq 0x60(%rsp), %rdi
callq 0x3170
callq 0x381e
movq %rax, %rdi
callq 0x665a
movl (%rbx), %edi
xorl %esi, %esi
xorl %edx, %edx
callq 0x30e0
movl %eax, %ebx
testl %eax, %eax
jns 0x4edc
callq 0x3c5a
leaq 0x322e(%rip), %rsi      # 0x80af
movq %rax, %rdi
callq 0x4576
leaq 0x333e(%rip), %rsi      # 0x81ce
movq %rax, %rdi
callq 0x4576
leaq 0x366f(%rip), %rsi      # 0x850e
movq %rax, %rdi
callq 0x4576
callq 0x381e
movq %rax, %r14
leaq 0x3327(%rip), %rsi      # 0x81dd
leaq 0x40(%rsp), %rdi
leaq 0xb(%rsp), %rdx
callq 0x3c1e
leaq 0x40(%rsp), %rsi
movq %r14, %rdi
callq 0x7060
leaq 0x40(%rsp), %rdi
callq 0x3170
callq 0x3c5a
leaq 0x324d(%rip), %rsi      # 0x8135
movq %rax, %rdi
callq 0x4576
leaq 0x32f4(%rip), %r15      # 0x81eb
movq %rax, %rdi
movq %r15, %rsi
callq 0x4576
leaq 0x3605(%rip), %rsi      # 0x850e
movq %rax, %rdi
callq 0x4576
callq 0x381e
movq %rax, %r14
leaq 0x20(%rsp), %rdi
leaq 0xb(%rsp), %rdx
movq %r15, %rsi
callq 0x3c1e
leaq 0x20(%rsp), %rsi
pushq $0x7
popq %rdx
movq %r14, %rdi
callq 0x6a20
leaq 0x20(%rsp), %rdi
callq 0x3170
pushq $0x1
popq %rdi
callq 0x3220
movl %ebx, %eax
addq $0x130, %rsp            # imm = 0x130
popq %rbx
popq %r14
popq %r15
retq
movq %rax, %rbx
leaq 0x40(%rsp), %rdi
jmp 0x4fc7
jmp 0x4fce
movq %rax, %rbx
leaq 0x80(%rsp), %rdi
jmp 0x4fc7
jmp 0x4fce
movq %rax, %rbx
leaq 0xc0(%rsp), %rdi
jmp 0x4fc7
movq %rax, %rbx
leaq 0xe0(%rsp), %rdi
jmp 0x4fc7
jmp 0x4fce
movq %rax, %rbx
leaq 0x100(%rsp), %rdi
jmp 0x4fc7
jmp 0x4fce
jmp 0x4fce
movq %rax, %rbx
leaq 0x20(%rsp), %rdi
jmp 0x4fc7
jmp 0x4fce
movq %rax, %rbx
leaq 0x60(%rsp), %rdi
jmp 0x4fc7
jmp 0x4fce
movq %rax, %rbx
leaq 0xa0(%rsp), %rdi
callq 0x3170
jmp 0x4fd1
movq %rax, %rbx
movq %rbx, %rdi
callq 0x34d0
nop