// File: Unreality3D[P]Unreality3D/indie-studio/source/Network/sockets/Server.cpp
// Function: Socket::Server::start(int, unsigned long)
void  Server::start(int port, size_t maxClients)
	{
		SOCKADDR_IN	addr;
		PROTOENT	*proto;

		// port checking
		if (port <= 0 || port > 0xFFFF)
			throw SocketCreateError(std::string("server : bad port"));
		_port = port;
		_maxClients = maxClients;

		// create socket
		proto = getprotobyname("tcp");

		if ((_fd = socket(AF_INET, SOCK_STREAM, proto ? proto->p_proto : 0)) == -1)
		  throw SocketCreateError(std::string("server : ") +
		  		std::string(strerror(getError())));

		if (!setNonBloccking(_fd))
			throw SocketCreateError(std::string("server : ") +
									std::string("cannot set non blocking mode for the server fd"));

		// bind
		memset(&addr, 0, sizeof(addr));
		addr.sin_family = AF_INET;
		addr.sin_addr.s_addr = htonl(INADDR_ANY);
		addr.sin_port = htons(port);

		if (bind(_fd, (sockaddr *)(&addr), sizeof(addr)) == -1)
		  throw SocketConnectError("server : " + std::string(strerror(getError())));
		if (listen(_fd, _maxClients) == -1)
		  throw SocketConnectError("server : " + std::string(strerror(getError())));

		// create select() sets
		FD_ZERO(&_fd_set);
		_max_fd = _fd;
		FD_SET(_fd, &_fd_set);

		// run the event loop
		_isRunning = true;
		if (!_stateThread.joinable())
			_stateThread = std::thread(&Server::stateChecker, this);

		// start event
		if (_OnStart)
			_OnStart(*this, _port);
	}

// Assembly:
subq $0x2a8, %rsp            # imm = 0x2A8
movq %rdi, 0x2a0(%rsp)
movl %esi, 0x29c(%rsp)
movq %rdx, 0x290(%rsp)
movq 0x2a0(%rsp), %rax
movq %rax, 0xa0(%rsp)
cmpl $0x0, 0x29c(%rsp)
jle 0x41fc9
cmpl $0xffff, 0x29c(%rsp) # imm = 0xFFFF
jle 0x420b7
movb $0x1, 0x243(%rsp)
movl $0x48, %edi
callq 0x28e70
movq %rax, 0x90(%rsp)
leaq 0x257(%rsp), %rdi
movq %rdi, 0x98(%rsp)
callq 0x2b730
movq 0x98(%rsp), %rdx
leaq 0x46c5(%rip), %rsi      # 0x466cc
leaq 0x258(%rsp), %rdi
callq 0x2aa80
jmp 0x42016
movq 0x90(%rsp), %rdi
leaq 0x258(%rsp), %rsi
callq 0x2a7b0
jmp 0x4202d
movq 0x90(%rsp), %rdi
movb $0x0, 0x243(%rsp)
movq 0x10f84(%rip), %rsi     # 0x52fc8
movq 0x10efd(%rip), %rdx     # 0x52f48
callq 0x2b540
jmp 0x42921
movq %rax, %rcx
movl %edx, %eax
movq %rcx, 0x248(%rsp)
movl %eax, 0x244(%rsp)
jmp 0x4208c
movq %rax, %rcx
movl %edx, %eax
movq %rcx, 0x248(%rsp)
movl %eax, 0x244(%rsp)
leaq 0x258(%rsp), %rdi
callq 0x29360
leaq 0x257(%rsp), %rdi
callq 0x2a210
testb $0x1, 0x243(%rsp)
jne 0x420a5
jmp 0x420b2
movq 0x90(%rsp), %rdi
callq 0x29690
jmp 0x42914
movq 0xa0(%rsp), %rax
movl 0x29c(%rsp), %ecx
movl %ecx, 0x8(%rax)
movq 0x290(%rsp), %rcx
movq %rcx, 0x10(%rax)
leaq 0x4486(%rip), %rdi      # 0x46562
callq 0x28ba0
movq %rax, 0x278(%rsp)
cmpq $0x0, 0x278(%rsp)
je 0x42108
movq 0x278(%rsp), %rax
movl 0x10(%rax), %eax
movl %eax, 0x8c(%rsp)
jmp 0x42113
xorl %eax, %eax
movl %eax, 0x8c(%rsp)
jmp 0x42113
movl 0x8c(%rsp), %edx
movl $0x2, %edi
movl $0x1, %esi
callq 0x285a0
movq 0xa0(%rsp), %rcx
movl %eax, 0x18(%rcx)
cmpl $-0x1, %eax
jne 0x422ee
movb $0x1, 0x1d6(%rsp)
movl $0x48, %edi
callq 0x28e70
movq %rax, 0x78(%rsp)
leaq 0x1ff(%rsp), %rdi
movq %rdi, 0x80(%rsp)
callq 0x2b730
movq 0x80(%rsp), %rdx
leaq 0x4566(%rip), %rsi      # 0x466de
leaq 0x200(%rsp), %rdi
callq 0x2aa80
jmp 0x42187
callq 0x42930
movl %eax, 0x74(%rsp)
jmp 0x42192
movl 0x74(%rsp), %edi
callq 0x28970
movq %rax, 0x60(%rsp)
leaq 0x1d7(%rsp), %rdi
movq %rdi, 0x68(%rsp)
callq 0x2b730
movq 0x60(%rsp), %rsi
movq 0x68(%rsp), %rdx
leaq 0x1d8(%rsp), %rdi
callq 0x2aa80
jmp 0x421cb
leaq 0x220(%rsp), %rdi
leaq 0x200(%rsp), %rsi
leaq 0x1d8(%rsp), %rdx
callq 0x29b70
jmp 0x421ea
movq 0x78(%rsp), %rdi
leaq 0x220(%rsp), %rsi
callq 0x2a7b0
jmp 0x421fe
movq 0x78(%rsp), %rdi
movb $0x0, 0x1d6(%rsp)
movq 0x10db6(%rip), %rsi     # 0x52fc8
movq 0x10d2f(%rip), %rdx     # 0x52f48
callq 0x2b540
jmp 0x42921
movq %rax, %rcx
movl %edx, %eax
movq %rcx, 0x248(%rsp)
movl %eax, 0x244(%rsp)
jmp 0x422c6
movq %rax, %rcx
movl %edx, %eax
movq %rcx, 0x248(%rsp)
movl %eax, 0x244(%rsp)
jmp 0x422b9
movq %rax, %rcx
movl %edx, %eax
movq %rcx, 0x248(%rsp)
movl %eax, 0x244(%rsp)
jmp 0x422ac
movq %rax, %rcx
movl %edx, %eax
movq %rcx, 0x248(%rsp)
movl %eax, 0x244(%rsp)
jmp 0x4229f
movq %rax, %rcx
movl %edx, %eax
movq %rcx, 0x248(%rsp)
movl %eax, 0x244(%rsp)
leaq 0x220(%rsp), %rdi
callq 0x29360
leaq 0x1d8(%rsp), %rdi
callq 0x29360
leaq 0x1d7(%rsp), %rdi
callq 0x2a210
leaq 0x200(%rsp), %rdi
callq 0x29360
leaq 0x1ff(%rsp), %rdi
callq 0x2a210
testb $0x1, 0x1d6(%rsp)
jne 0x422df
jmp 0x422e9
movq 0x78(%rsp), %rdi
callq 0x29690
jmp 0x42914
movq 0xa0(%rsp), %rax
movl 0x18(%rax), %edi
callq 0x42940
testb $0x1, %al
jne 0x42481
movb $0x1, 0x166(%rsp)
movl $0x48, %edi
callq 0x28e70
movq %rax, 0x50(%rsp)
leaq 0x18f(%rsp), %rdi
movq %rdi, 0x58(%rsp)
callq 0x2b730
movq 0x58(%rsp), %rdx
leaq 0x43a3(%rip), %rsi      # 0x466de
leaq 0x190(%rsp), %rdi
callq 0x2aa80
jmp 0x4234a
leaq 0x167(%rsp), %rdi
movq %rdi, 0x48(%rsp)
callq 0x2b730
movq 0x48(%rsp), %rdx
leaq 0x4380(%rip), %rsi      # 0x466e8
leaq 0x168(%rsp), %rdi
callq 0x2aa80
jmp 0x42377
leaq 0x1b0(%rsp), %rdi
leaq 0x190(%rsp), %rsi
leaq 0x168(%rsp), %rdx
callq 0x29b70
jmp 0x42396
movq 0x50(%rsp), %rdi
leaq 0x1b0(%rsp), %rsi
callq 0x2a7b0
jmp 0x423aa
movq 0x50(%rsp), %rdi
movb $0x0, 0x166(%rsp)
movq 0x10c0a(%rip), %rsi     # 0x52fc8
movq 0x10b83(%rip), %rdx     # 0x52f48
callq 0x2b540
jmp 0x42921
movq %rax, %rcx
movl %edx, %eax
movq %rcx, 0x248(%rsp)
movl %eax, 0x244(%rsp)
jmp 0x42459
movq %rax, %rcx
movl %edx, %eax
movq %rcx, 0x248(%rsp)
movl %eax, 0x244(%rsp)
jmp 0x4243f
movq %rax, %rcx
movl %edx, %eax
movq %rcx, 0x248(%rsp)
movl %eax, 0x244(%rsp)
jmp 0x42432
movq %rax, %rcx
movl %edx, %eax
movq %rcx, 0x248(%rsp)
movl %eax, 0x244(%rsp)
leaq 0x1b0(%rsp), %rdi
callq 0x29360
leaq 0x168(%rsp), %rdi
callq 0x29360
leaq 0x167(%rsp), %rdi
callq 0x2a210
leaq 0x190(%rsp), %rdi
callq 0x29360
leaq 0x18f(%rsp), %rdi
callq 0x2a210
testb $0x1, 0x166(%rsp)
jne 0x42472
jmp 0x4247c
movq 0x50(%rsp), %rdi
callq 0x29690
jmp 0x42914
leaq 0x280(%rsp), %rdi
xorl %esi, %esi
movl $0x10, %edx
callq 0x29060
movw $0x2, 0x280(%rsp)
xorl %edi, %edi
callq 0x297a0
movl %eax, 0x284(%rsp)
movl 0x29c(%rsp), %eax
movzwl %ax, %edi
callq 0x28140
movw %ax, %cx
movq 0xa0(%rsp), %rax
movw %cx, 0x282(%rsp)
movl 0x18(%rax), %edi
leaq 0x280(%rsp), %rsi
movl $0x10, %edx
callq 0x28290
cmpl $-0x1, %eax
jne 0x42637
movb $0x1, 0x11e(%rsp)
movl $0x48, %edi
callq 0x28e70
movq %rax, 0x38(%rsp)
callq 0x42930
movl %eax, 0x44(%rsp)
jmp 0x4250f
movl 0x44(%rsp), %edi
callq 0x28970
movq %rax, 0x28(%rsp)
leaq 0x11f(%rsp), %rdi
movq %rdi, 0x30(%rsp)
callq 0x2b730
movq 0x28(%rsp), %rsi
movq 0x30(%rsp), %rdx
leaq 0x120(%rsp), %rdi
callq 0x2aa80
jmp 0x42548
leaq 0x418f(%rip), %rsi      # 0x466de
leaq 0x140(%rsp), %rdi
leaq 0x120(%rsp), %rdx
callq 0x2b480
jmp 0x42566
movq 0x38(%rsp), %rdi
leaq 0x140(%rsp), %rsi
callq 0x2a230
jmp 0x4257a
movq 0x38(%rsp), %rdi
movb $0x0, 0x11e(%rsp)
movq 0x109b2(%rip), %rsi     # 0x52f40
movq 0x109bb(%rip), %rdx     # 0x52f50
callq 0x2b540
jmp 0x42921
movq %rax, %rcx
movl %edx, %eax
movq %rcx, 0x248(%rsp)
movl %eax, 0x244(%rsp)
jmp 0x4261c
movq %rax, %rcx
movl %edx, %eax
movq %rcx, 0x248(%rsp)
movl %eax, 0x244(%rsp)
jmp 0x4260f
movq %rax, %rcx
movl %edx, %eax
movq %rcx, 0x248(%rsp)
movl %eax, 0x244(%rsp)
jmp 0x42602
movq %rax, %rcx
movl %edx, %eax
movq %rcx, 0x248(%rsp)
movl %eax, 0x244(%rsp)
leaq 0x140(%rsp), %rdi
callq 0x29360
leaq 0x120(%rsp), %rdi
callq 0x29360
leaq 0x11f(%rsp), %rdi
callq 0x2a210
testb $0x1, 0x11e(%rsp)
jne 0x42628
jmp 0x42632
movq 0x38(%rsp), %rdi
callq 0x29690
jmp 0x42914
movq 0xa0(%rsp), %rax
movl 0x18(%rax), %edi
movq 0x10(%rax), %rax
movl %eax, %esi
callq 0x2a3d0
cmpl $-0x1, %eax
jne 0x427a0
movb $0x1, 0xd6(%rsp)
movl $0x48, %edi
callq 0x28e70
movq %rax, 0x18(%rsp)
callq 0x42930
movl %eax, 0x24(%rsp)
jmp 0x42678
movl 0x24(%rsp), %edi
callq 0x28970
movq %rax, 0x8(%rsp)
leaq 0xd7(%rsp), %rdi
movq %rdi, 0x10(%rsp)
callq 0x2b730
movq 0x8(%rsp), %rsi
movq 0x10(%rsp), %rdx
leaq 0xd8(%rsp), %rdi
callq 0x2aa80
jmp 0x426b1
leaq 0x4026(%rip), %rsi      # 0x466de
leaq 0xf8(%rsp), %rdi
leaq 0xd8(%rsp), %rdx
callq 0x2b480
jmp 0x426cf
movq 0x18(%rsp), %rdi
leaq 0xf8(%rsp), %rsi
callq 0x2a230
jmp 0x426e3
movq 0x18(%rsp), %rdi
movb $0x0, 0xd6(%rsp)
movq 0x10849(%rip), %rsi     # 0x52f40
movq 0x10852(%rip), %rdx     # 0x52f50
callq 0x2b540
jmp 0x42921
movq %rax, %rcx
movl %edx, %eax
movq %rcx, 0x248(%rsp)
movl %eax, 0x244(%rsp)
jmp 0x42785
movq %rax, %rcx
movl %edx, %eax
movq %rcx, 0x248(%rsp)
movl %eax, 0x244(%rsp)
jmp 0x42778
movq %rax, %rcx
movl %edx, %eax
movq %rcx, 0x248(%rsp)
movl %eax, 0x244(%rsp)
jmp 0x4276b
movq %rax, %rcx
movl %edx, %eax
movq %rcx, 0x248(%rsp)
movl %eax, 0x244(%rsp)
leaq 0xf8(%rsp), %rdi
callq 0x29360
leaq 0xd8(%rsp), %rdi
callq 0x29360
leaq 0xd7(%rsp), %rdi
callq 0x2a210
testb $0x1, 0xd6(%rsp)
jne 0x42791
jmp 0x4279b
movq 0x18(%rsp), %rdi
callq 0x29690
jmp 0x42914
jmp 0x427a2
movq 0xa0(%rsp), %rax
addq $0xf0, %rax
movq %rax, 0xc8(%rsp)
movl $0x0, 0xd0(%rsp)
movl 0xd0(%rsp), %eax
cmpq $0x10, %rax
jae 0x427fa
movq 0xc8(%rsp), %rax
movl 0xd0(%rsp), %ecx
movq $0x0, (%rax,%rcx,8)
movl 0xd0(%rsp), %eax
addl $0x1, %eax
movl %eax, 0xd0(%rsp)
jmp 0x427c3
jmp 0x427fc
movq 0xa0(%rsp), %rdi
movl 0x18(%rdi), %eax
movl %eax, 0x170(%rdi)
movl 0x18(%rdi), %eax
movl $0x40, %ecx
cltd
idivl %ecx
movl %edx, %eax
movl %eax, %ecx
movl $0x1, %eax
shlq %cl, %rax
movq %rax, %rcx
movl 0x18(%rdi), %eax
movl $0x40, %esi
cltd
idivl %esi
cltq
orq 0xf0(%rdi,%rax,8), %rcx
movq %rcx, 0xf0(%rdi,%rax,8)
addq $0x174, %rdi            # imm = 0x174
movl $0x1, %esi
callq 0x289c0
movq 0xa0(%rsp), %rdi
addq $0x38, %rdi
callq 0x28560
testb $0x1, %al
jne 0x428d8
movq 0xa0(%rsp), %rax
movq 0x10707(%rip), %rcx     # 0x52f80
movq %rcx, 0xb0(%rsp)
movq $0x0, 0xb8(%rsp)
movq %rax, 0xa8(%rsp)
leaq 0xc0(%rsp), %rdi
leaq 0xb0(%rsp), %rsi
leaq 0xa8(%rsp), %rdx
callq 0x28430
movq 0xa0(%rsp), %rdi
addq $0x38, %rdi
leaq 0xc0(%rsp), %rsi
callq 0x2b350
leaq 0xc0(%rsp), %rdi
callq 0x2b010
movq 0xa0(%rsp), %rdi
addq $0xd0, %rdi
callq 0x29b50
testb $0x1, %al
jne 0x428f2
jmp 0x4290c
movq 0xa0(%rsp), %rsi
movq %rsi, %rdi
addq $0xd0, %rdi
movl 0x8(%rsi), %edx
callq 0x2a300
addq $0x2a8, %rsp            # imm = 0x2A8
retq
movq 0x248(%rsp), %rdi
callq 0x2b6f0
nopw %cs:(%rax,%rax)