// File: XuhuiSun95[P]Ticket-Booking-Service/server/ServerSocket.cpp
// Function: ServerSocket::Init(std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char>> const&, int const&, bool)
void ServerSocket::Init(const std::string &ipaddress, const int &port, bool host) {

    if((mSockfd = socket(AF_INET, SOCK_STREAM, 0))==0) {
        std::cout << "Socket creation error" << std::endl;
        return;
    }
    
    //setup socket address structure
    memset(&mAddress, '0', sizeof(mAddress));
    mAddress.sin_family = AF_INET;
    mAddress.sin_port = htons(port);

    if(host) {

        mAddress.sin_addr.s_addr = INADDR_ANY;

        // call bind to associate the socket with our local address and port
        if(bind(mSockfd, (struct sockaddr *)&mAddress, sizeof(mAddress))<0) {
            std::cout << "Bind Failed" << std::endl;
            return;
        }

        // convert the socket to listen for incoming connections
        if(listen(mSockfd, 10)<0) {
            std::cout << "Listen Failed" << std::endl;
            return;
        }
    }
    else {

        if(inet_pton(AF_INET, ipaddress.c_str(), &mAddress.sin_addr)<=0) {
            std::cout << "Invalid pair server address/ Address not supported" << std::endl;
            return;
        }

        if(connect(mSockfd, (struct sockaddr *)&mAddress, sizeof(mAddress))<0) {
            std::cout << "Connection Failed" << std::endl;
            return;
        }
    }

    mValid = true;
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
subq $0x30, %rsp
movb %cl, %al
movq %rdi, -0x8(%rbp)
movq %rsi, -0x10(%rbp)
movq %rdx, -0x18(%rbp)
andb $0x1, %al
movb %al, -0x19(%rbp)
movq -0x8(%rbp), %rax
movq %rax, -0x28(%rbp)
movl $0x2, %edi
movl $0x1, %esi
xorl %edx, %edx
callq 0x2070
movq -0x28(%rbp), %rcx
movl %eax, 0x4(%rcx)
cmpl $0x0, %eax
jne 0x52d7
movq 0x4d11(%rip), %rdi      # 0x9fc8
leaq 0x10fb(%rip), %rsi      # 0x63b9
callq 0x21f0
movq %rax, %rdi
movq 0x4ce3(%rip), %rsi      # 0x9fb0
callq 0x2220
jmp 0x5437
movq -0x28(%rbp), %rdi
addq $0xc, %rdi
movl $0x30, %esi
movl $0x10, %edx
callq 0x2120
movq -0x28(%rbp), %rax
movw $0x2, 0xc(%rax)
movq -0x18(%rbp), %rax
movl (%rax), %eax
movzwl %ax, %edi
callq 0x2050
movw %ax, %cx
movq -0x28(%rbp), %rax
movw %cx, 0xe(%rax)
testb $0x1, -0x19(%rbp)
je 0x53a5
movq -0x28(%rbp), %rsi
movl $0x0, 0x10(%rsi)
movl 0x4(%rsi), %edi
addq $0xc, %rsi
movl $0x10, %edx
callq 0x2060
cmpl $0x0, %eax
jge 0x5363
movq 0x4c85(%rip), %rdi      # 0x9fc8
leaq 0x1085(%rip), %rsi      # 0x63cf
callq 0x21f0
movq %rax, %rdi
movq 0x4c57(%rip), %rsi      # 0x9fb0
callq 0x2220
jmp 0x5437
movq -0x28(%rbp), %rax
movl 0x4(%rax), %edi
movl $0xa, %esi
callq 0x2240
cmpl $0x0, %eax
jge 0x53a0
movq 0x4c48(%rip), %rdi      # 0x9fc8
leaq 0x1054(%rip), %rsi      # 0x63db
callq 0x21f0
movq %rax, %rdi
movq 0x4c1a(%rip), %rsi      # 0x9fb0
callq 0x2220
jmp 0x5437
jmp 0x5430
movq -0x10(%rbp), %rdi
callq 0x20e0
movq -0x28(%rbp), %rdx
movq %rax, %rsi
addq $0xc, %rdx
addq $0x4, %rdx
movl $0x2, %edi
callq 0x2370
cmpl $0x0, %eax
jg 0x53f0
movq 0x4bf5(%rip), %rdi      # 0x9fc8
leaq 0x100f(%rip), %rsi      # 0x63e9
callq 0x21f0
movq %rax, %rdi
movq 0x4bc7(%rip), %rsi      # 0x9fb0
callq 0x2220
jmp 0x5437
movq -0x28(%rbp), %rsi
movl 0x4(%rsi), %edi
addq $0xc, %rsi
movl $0x10, %edx
callq 0x21a0
cmpl $0x0, %eax
jge 0x542e
movq 0x4bb7(%rip), %rdi      # 0x9fc8
leaq 0x1004(%rip), %rsi      # 0x641c
callq 0x21f0
movq %rax, %rdi
movq 0x4b89(%rip), %rsi      # 0x9fb0
callq 0x2220
jmp 0x5437
jmp 0x5430
movq -0x28(%rbp), %rax
movb $0x1, (%rax)
addq $0x30, %rsp
popq %rbp
retq
nopl (%rax)