// File: Neuromancer2701[P]OpenTool/lib/sockets/Client.cpp
// Function: Client::Accept(int)
Error Client::Accept(int _server_fd){
	struct sockaddr in_addr;
	socklen_t in_len;
	int local_client_fd;
	char port_buffer[32];
	char ip_buffer[32];
	Error error_state = Error::NONE;

	DEBUG("Accepting, Server FD: %d\n ",_server_fd);
	in_len = sizeof( in_addr);
	local_client_fd = accept (_server_fd, &in_addr, &in_len);
	if (local_client_fd == -1)
	{
		if ((errno == EAGAIN) || (errno == EWOULDBLOCK))  /* We have processed all incoming connections. */
		{
			status = Connection_Status::UNKNOWN;
			error_state = Error::ACCEPT_DONE;
			client_fd = -1;
		}
		else
		{
			perror ("accept");
			status = Connection_Status::DISCONNECTED;
			error_state = Error::ACCEPT;
			client_fd = -1;
		}
	}
	else
	{
		client_fd = local_client_fd;
		status = Connection_Status::CONNECTED;
		retries = 0;

		DEBUG("Accepted connections, client FD: %d\n ",client_fd);

        int nameinfo = getnameinfo (&in_addr, in_len, ip_buffer, sizeof(ip_buffer), port_buffer, sizeof(port_buffer), NI_NUMERICHOST | NI_NUMERICSERV);
		if (nameinfo == 0)
		{
			ip_address = string(ip_buffer);
			port = atoi(port_buffer);
			DEBUG("Client Ip:%s:%d \n ",ip_address.c_str(), port);
		}

		if(setBlockingMode(Blocking_Mode::NonBlock) != Error::NONE)
		{
			perror("Nonblock failed.");
			status = Connection_Status::DISCONNECTED;
		}
	}

	return error_state;
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
subq $0xd0, %rsp
movq %rdi, -0x8(%rbp)
movl %esi, -0xc(%rbp)
movq -0x8(%rbp), %rax
movq %rax, -0xb8(%rbp)
movl $0x2, -0x74(%rbp)
movl -0xc(%rbp), %esi
leaq 0x3225(%rip), %rdi      # 0x13133
movb $0x0, %al
callq 0xa070
movl $0x10, -0x20(%rbp)
movl -0xc(%rbp), %edi
leaq -0x1c(%rbp), %rsi
leaq -0x20(%rbp), %rdx
callq 0xa2a0
movl %eax, -0x24(%rbp)
cmpl $-0x1, -0x24(%rbp)
jne 0xff94
callq 0xa060
cmpl $0xb, (%rax)
je 0xff49
callq 0xa060
cmpl $0xb, (%rax)
jne 0xff67
movq -0xb8(%rbp), %rax
movl $0x0, 0x8(%rax)
movl $0x10, -0x74(%rbp)
movl $0xffffffff, 0xc(%rax)  # imm = 0xFFFFFFFF
jmp 0xff8f
leaq 0x31e0(%rip), %rdi      # 0x1314e
callq 0xa720
movq -0xb8(%rbp), %rax
movl $0xffffffff, 0x8(%rax)  # imm = 0xFFFFFFFF
movl $0x9, -0x74(%rbp)
movl $0xffffffff, 0xc(%rax)  # imm = 0xFFFFFFFF
jmp 0x100d6
movq -0xb8(%rbp), %rax
movl -0x24(%rbp), %ecx
movl %ecx, 0xc(%rax)
movl $0x1, 0x8(%rax)
movb $0x0, 0x10(%rax)
movl 0xc(%rax), %esi
leaq 0x319f(%rip), %rdi      # 0x13155
movb $0x0, %al
callq 0xa070
movl -0x20(%rbp), %esi
leaq -0x70(%rbp), %rdx
leaq -0x50(%rbp), %r8
leaq -0x1c(%rbp), %rdi
movl $0x20, %r9d
movl %r9d, %ecx
movl $0x3, (%rsp)
callq 0xa560
movl %eax, -0x78(%rbp)
cmpl $0x0, -0x78(%rbp)
jne 0x100a4
leaq -0x99(%rbp), %rdi
movq %rdi, -0xc0(%rbp)
callq 0xaea0
movq -0xc0(%rbp), %rdx
leaq -0x98(%rbp), %rdi
leaq -0x70(%rbp), %rsi
callq 0xab20
jmp 0x1001a
movq -0xb8(%rbp), %rdi
addq $0x18, %rdi
leaq -0x98(%rbp), %rsi
callq 0xa9c0
leaq -0x98(%rbp), %rdi
callq 0xa540
leaq -0x99(%rbp), %rdi
callq 0xa920
leaq -0x50(%rbp), %rdi
callq 0xacc0
movq -0xb8(%rbp), %rdi
movw %ax, 0x38(%rdi)
addq $0x18, %rdi
callq 0xa350
movq %rax, %rsi
movq -0xb8(%rbp), %rax
movzwl 0x38(%rax), %edx
leaq 0x3100(%rip), %rdi      # 0x1317b
movb $0x0, %al
callq 0xa070
jmp 0x100a4
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0xa8(%rbp)
movl %eax, -0xac(%rbp)
leaq -0x99(%rbp), %rdi
callq 0xa920
jmp 0x100e2
movq -0xb8(%rbp), %rdi
movl $0x1, %esi
callq 0xaf80
cmpl $0x2, %eax
je 0x100d4
leaq 0x3244(%rip), %rdi      # 0x13305
callq 0xa720
movq -0xb8(%rbp), %rax
movl $0xffffffff, 0x8(%rax)  # imm = 0xFFFFFFFF
jmp 0x100d6
movl -0x74(%rbp), %eax
addq $0xd0, %rsp
popq %rbp
retq
movq -0xa8(%rbp), %rdi
callq 0xae90
nop