// File: Neuromancer2701[P]OpenTool/lib/sockets/Server.cpp
// Function: Server::Server(int)
Server::Server(int _port)	{

	active_connections = 0;
	server_fd = socket(AF_INET, SOCK_STREAM, 0);
    if( server_fd < 0 )
	{
		perror("socket");
		error_state = Error::SOCKET;
	}
    else
    {
	    int optval = 1;
		struct sockaddr_in local_address;
		memset((char *)&local_address, 0, sizeof(local_address));
		local_address.sin_family = AF_INET;
		local_address.sin_addr.s_addr = htonl(INADDR_ANY);
		local_address.sin_port = htons(_port);

	    setsockopt(server_fd, SOL_SOCKET, SO_REUSEPORT, &optval, sizeof(optval));

		if ( bind(server_fd, (struct sockaddr*)&local_address, sizeof(local_address)) != 0 )
		{
			perror("socket--bind");
			error_state = Error::BIND;
			server_fd = -1;
		}
		else
		{
			error_state = setBlockingMode(Blocking_Mode::NonBlock);
			active_connections = 0;
			if(error_state != Error::NONE)
			{
				perror("server Nonblock failed.");
				server_fd = -1;
			}
			else
			{
				if ( listen(server_fd, MAX_CONNECTIONS) != 0 )
				{
					perror("socket--listen");
					error_state = Error::LISTEN;
					server_fd = -1;
				}
				else
				{
					int epoll_status;
					event_fd = epoll_create1 (0);
					if (event_fd == -1)
					{
						perror ("epoll_create");
						error_state = Error::EPOLL_CREATE;
						server_fd = -1;
					}
					else
					{
						event.data.fd = server_fd;
						event.events = EPOLLIN;

						epoll_status = epoll_ctl (event_fd, EPOLL_CTL_ADD, server_fd, &event);
						if (epoll_status == -1)
						{
							perror ("epoll_create");
							error_state = Error::EPOLL_CTL;
							server_fd = -1;
						}
						else
						{
							//DEBUG("Created server object, bind, setblocking mode, Listening, Server FD: %d\n ",server_fd);
							char port_buffer[32];
							char ip_buffer[32];

							socklen_t len = sizeof( local_address);
							int nameinfo = getnameinfo ((const sockaddr *)&local_address, len, ip_buffer, sizeof(ip_buffer), port_buffer, sizeof(port_buffer), NI_NUMERICHOST | NI_NUMERICSERV);
							if (nameinfo == 0)
							{
								string ip_address = string(ip_buffer);
								port = atoi(port_buffer);
								DEBUG("Server Ip:%s:%d \n ",ip_address.c_str(), port);
							}
						}
					}
				}
			}
		}
    }
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
subq $0xf0, %rsp
movq %rdi, -0x8(%rbp)
movl %esi, -0xc(%rbp)
movq -0x8(%rbp), %rdi
movq %rdi, -0xc0(%rbp)
movq 0x6b4c(%rip), %rax      # 0x17fa0
addq $0x10, %rax
movq %rax, (%rdi)
addq $0x10, %rdi
movq %rdi, -0xd0(%rbp)
callq 0xa800
movq -0xc0(%rbp), %rdi
addq $0x190, %rdi            # imm = 0x190
movq %rdi, -0xc8(%rbp)
callq 0xa6c0
movq -0xc0(%rbp), %rdi
addq $0x288, %rdi            # imm = 0x288
movq %rdi, -0xb8(%rbp)
callq 0xa110
jmp 0x114a1
movq -0xc0(%rbp), %rax
movl $0x0, 0x8(%rax)
movl $0x2, %edi
movl $0x1, %esi
xorl %edx, %edx
callq 0xa1e0
movl %eax, %ecx
movq -0xc0(%rbp), %rax
movl %ecx, 0x1a8(%rax)
cmpl $0x0, 0x1a8(%rax)
jge 0x1151e
leaq 0x1cd0(%rip), %rdi      # 0x131af
callq 0xa720
jmp 0x114e6
movq -0xc0(%rbp), %rax
movl $0x7, 0x1b4(%rax)
jmp 0x11851
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x18(%rbp)
movl %eax, -0x1c(%rbp)
jmp 0x11866
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x18(%rbp)
movl %eax, -0x1c(%rbp)
jmp 0x1185a
movl $0x1, -0x20(%rbp)
leaq -0x30(%rbp), %rdi
xorl %esi, %esi
movl $0x10, %edx
callq 0xa470
movw $0x2, -0x30(%rbp)
xorl %edi, %edi
callq 0xa670
movl %eax, -0x2c(%rbp)
movl -0xc(%rbp), %eax
movzwl %ax, %edi
callq 0xa0a0
movw %ax, %cx
movq -0xc0(%rbp), %rax
movw %cx, -0x2e(%rbp)
movl 0x1a8(%rax), %edi
movl $0x1, %esi
movl $0xf, %edx
leaq -0x20(%rbp), %rcx
movl $0x4, %r8d
callq 0xaed0
movq -0xc0(%rbp), %rax
movl 0x1a8(%rax), %edi
leaq -0x30(%rbp), %rsi
movl $0x10, %edx
callq 0xa0e0
cmpl $0x0, %eax
je 0x115cb
leaq 0x1d4d(%rip), %rdi      # 0x132f1
callq 0xa720
jmp 0x115ab
movq -0xc0(%rbp), %rax
movl $0x8, 0x1b4(%rax)
movl $0xffffffff, 0x1a8(%rax) # imm = 0xFFFFFFFF
jmp 0x1184f
movq -0xc0(%rbp), %rdi
movl $0x1, %esi
callq 0xa410
movl %eax, -0xd4(%rbp)
jmp 0x115e4
movq -0xc0(%rbp), %rax
movl -0xd4(%rbp), %ecx
movl %ecx, 0x1b4(%rax)
movl $0x0, 0x8(%rax)
cmpl $0x2, 0x1b4(%rax)
je 0x1162b
leaq 0x1cf0(%rip), %rdi      # 0x132fe
callq 0xa720
jmp 0x11615
movq -0xc0(%rbp), %rax
movl $0xffffffff, 0x1a8(%rax) # imm = 0xFFFFFFFF
jmp 0x1184d
movq -0xc0(%rbp), %rax
movl 0x1a8(%rax), %edi
movl $0x6, %esi
callq 0xa940
cmpl $0x0, %eax
je 0x11675
leaq 0x1cc8(%rip), %rdi      # 0x13316
callq 0xa720
jmp 0x11655
movq -0xc0(%rbp), %rax
movl $0xa, 0x1b4(%rax)
movl $0xffffffff, 0x1a8(%rax) # imm = 0xFFFFFFFF
jmp 0x1184b
xorl %edi, %edi
callq 0xaaa0
movl %eax, %ecx
movq -0xc0(%rbp), %rax
movl %ecx, 0x1b8(%rax)
cmpl $-0x1, 0x1b8(%rax)
jne 0x116c2
leaq 0x1bd3(%rip), %rdi      # 0x1326e
callq 0xa720
jmp 0x116a2
movq -0xc0(%rbp), %rax
movl $0xb, 0x1b4(%rax)
movl $0xffffffff, 0x1a8(%rax) # imm = 0xFFFFFFFF
jmp 0x11849
movq -0xc0(%rbp), %rcx
movl 0x1a8(%rcx), %eax
movl %eax, 0x1c0(%rcx)
movl $0x1, 0x1bc(%rcx)
movl 0x1b8(%rcx), %edi
movl 0x1a8(%rcx), %edx
addq $0x1bc, %rcx            # imm = 0x1BC
movl $0x1, %esi
callq 0xab60
movl %eax, -0x34(%rbp)
cmpl $-0x1, -0x34(%rbp)
jne 0x11733
leaq 0x1b62(%rip), %rdi      # 0x1326e
callq 0xa720
jmp 0x11713
movq -0xc0(%rbp), %rax
movl $0xc, 0x1b4(%rax)
movl $0xffffffff, 0x1a8(%rax) # imm = 0xFFFFFFFF
jmp 0x11847
movl $0x10, -0x84(%rbp)
movl -0x84(%rbp), %esi
movq %rsp, %rax
movl $0x3, (%rax)
leaq -0x30(%rbp), %rdi
leaq -0x80(%rbp), %rdx
leaq -0x60(%rbp), %r8
movl $0x20, %r9d
movl %r9d, %ecx
callq 0xa560
movl %eax, -0xd8(%rbp)
jmp 0x1176e
movl -0xd8(%rbp), %eax
movl %eax, -0x88(%rbp)
cmpl $0x0, -0x88(%rbp)
jne 0x11845
leaq -0xa9(%rbp), %rdi
movq %rdi, -0xe0(%rbp)
callq 0xaea0
movq -0xe0(%rbp), %rdx
leaq -0xa8(%rbp), %rdi
leaq -0x80(%rbp), %rsi
callq 0xab20
jmp 0x117b3
leaq -0xa9(%rbp), %rdi
callq 0xa920
leaq -0x60(%rbp), %rdi
callq 0xacc0
movl %eax, %ecx
movq -0xc0(%rbp), %rax
movl %ecx, 0x1ac(%rax)
leaq -0xa8(%rbp), %rdi
callq 0xa350
movq %rax, %rsi
movq -0xc0(%rbp), %rax
movl 0x1ac(%rax), %edx
leaq 0x1b2b(%rip), %rdi      # 0x13325
xorl %eax, %eax
callq 0xa070
jmp 0x11803
leaq -0xa8(%rbp), %rdi
callq 0xa540
jmp 0x11845
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x18(%rbp)
movl %eax, -0x1c(%rbp)
leaq -0xa9(%rbp), %rdi
callq 0xa920
jmp 0x1185a
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x18(%rbp)
movl %eax, -0x1c(%rbp)
leaq -0xa8(%rbp), %rdi
callq 0xa540
jmp 0x1185a
jmp 0x11847
jmp 0x11849
jmp 0x1184b
jmp 0x1184d
jmp 0x1184f
jmp 0x11851
addq $0xf0, %rsp
popq %rbp
retq
movq -0xb8(%rbp), %rdi
callq 0xae00
movq -0xc8(%rbp), %rdi
callq 0xa690
movq -0xd0(%rbp), %rdi
callq 0xa1c0
movq -0x18(%rbp), %rdi
callq 0xae90
nopw (%rax,%rax)