// File: Unreality3D[P]Unreality3D/indie-studio/source/Network/sockets/Server.cpp
// Function: Socket::Server::stateChecker()
void Server::stateChecker()
	{
		int fd;
		int nb_fd;
		fd_set read_set, write_set;
		timeval timeout;

		// event loop
		while (_isRunning)
		{
		  SLEEPMS(0);
		  memcpy(&read_set, &_fd_set, sizeof(_fd_set));
		  memcpy(&write_set, &_fd_set, sizeof(_fd_set));

			// wait for an event
			timeout = _timeout;
			nb_fd = select(_max_fd + 1, &read_set, &write_set, NULL, &timeout);
			if (nb_fd < 0 && errno != EINTR)
				throw SocketIOError(std::string(strerror(getError())));

			if (FD_ISSET(_fd, &read_set))
			{
				// connection event
				while ((fd = ::accept(_fd, NULL, NULL)) != -1)
				{
					if (!setNonBloccking(fd))
						throw SocketCreateError(std::string("server : ") +
												std::string("cannot set non blocking mode for the client fd"));

					_clients.push_back(fd);
					FD_SET(fd, &_fd_set);
					_max_fd = (fd > _max_fd) ? fd : _max_fd;
					if (_OnConnect)
						_OnConnect(*this, fd);
				}
				if (errno != EAGAIN && errno != EWOULDBLOCK)
					throw SocketConnectError(std::string(strerror(getError())));
			}

			for (auto it = _clients.begin(); it != _clients.end() && _clients.size(); ++it)
			{
				// read event
				if (FD_ISSET(*it, &read_set))
				{
				  if (*it == _fd);
				  else
				    {
				      if (!bytesAvailables(*it))
					{
					  disconnect(*it);
					  it = _clients.begin();
					  continue;
					}
				      else if (_OnReadPossible)
					_OnReadPossible(*this, *it, bytesAvailables(*it));
				    }
				}
				// write event
				if (FD_ISSET(*it, &write_set))
				{
					if (*it == _fd);
					else if (_OnWritePossible)
						_OnWritePossible(*this, *it);
					/*
					else
						std::cout << "no callback set for OnWritePossible" << std::endl;
						*/
				}
			}
		}
	}

// Assembly:
subq $0x2a8, %rsp            # imm = 0x2A8
movq %rdi, 0x2a0(%rsp)
movq 0x2a0(%rsp), %rax
movq %rax, 0xa0(%rsp)
movq 0xa0(%rsp), %rdi
addq $0x174, %rdi            # imm = 0x174
callq 0x29500
testb $0x1, %al
jne 0x429dc
jmp 0x43289
xorl %edi, %edi
callq 0x2ac80
movq 0xa0(%rsp), %rsi
addq $0xf0, %rsi
leaq 0x218(%rsp), %rdi
movl $0x80, %edx
callq 0x29a50
movq 0xa0(%rsp), %rsi
addq $0xf0, %rsi
leaq 0x198(%rsp), %rdi
movl $0x80, %edx
callq 0x29a50
movq 0xa0(%rsp), %rax
movq 0x40(%rax), %rcx
movq %rcx, 0x188(%rsp)
movq 0x48(%rax), %rcx
movq %rcx, 0x190(%rsp)
movl 0x170(%rax), %edi
addl $0x1, %edi
leaq 0x218(%rsp), %rsi
leaq 0x198(%rsp), %rdx
xorl %eax, %eax
movl %eax, %ecx
leaq 0x188(%rsp), %r8
callq 0x29ed0
movl %eax, 0x298(%rsp)
cmpl $0x0, 0x298(%rsp)
jge 0x42bb9
callq 0x280d0
cmpl $0x4, (%rax)
je 0x42bb9
movb $0x1, 0x15a(%rsp)
movl $0x48, %edi
callq 0x28e70
movq %rax, 0x90(%rsp)
callq 0x42930
movl %eax, 0x9c(%rsp)
jmp 0x42aba
movl 0x9c(%rsp), %edi
callq 0x28970
movq %rax, 0x80(%rsp)
leaq 0x15b(%rsp), %rdi
movq %rdi, 0x88(%rsp)
callq 0x2b730
movq 0x80(%rsp), %rsi
movq 0x88(%rsp), %rdx
leaq 0x168(%rsp), %rdi
callq 0x2aa80
jmp 0x42b02
movq 0x90(%rsp), %rdi
leaq 0x168(%rsp), %rsi
callq 0x2afd0
jmp 0x42b19
movq 0x90(%rsp), %rdi
movb $0x0, 0x15a(%rsp)
movq 0x10458(%rip), %rsi     # 0x52f88
movq 0x103f9(%rip), %rdx     # 0x52f30
callq 0x2b540
jmp 0x4329e
movq %rax, %rcx
movl %edx, %eax
movq %rcx, 0x160(%rsp)
movl %eax, 0x15c(%rsp)
jmp 0x42b9b
movq %rax, %rcx
movl %edx, %eax
movq %rcx, 0x160(%rsp)
movl %eax, 0x15c(%rsp)
jmp 0x42b8e
movq %rax, %rcx
movl %edx, %eax
movq %rcx, 0x160(%rsp)
movl %eax, 0x15c(%rsp)
leaq 0x168(%rsp), %rdi
callq 0x29360
leaq 0x15b(%rsp), %rdi
callq 0x2a210
testb $0x1, 0x15a(%rsp)
jne 0x42ba7
jmp 0x42bb4
movq 0x90(%rsp), %rdi
callq 0x29690
jmp 0x43291
movq 0xa0(%rsp), %rax
movl 0x18(%rax), %eax
movl $0x40, %ecx
cltd
idivl %ecx
movl %eax, %ecx
movq 0xa0(%rsp), %rax
movslq %ecx, %rcx
movq 0x218(%rsp,%rcx,8), %rcx
movq %rcx, 0x78(%rsp)
movl 0x18(%rax), %eax
movl $0x40, %ecx
cltd
idivl %ecx
movq 0x78(%rsp), %rax
movl %edx, %ecx
movl $0x1, %edx
shlq %cl, %rdx
movq %rdx, %rcx
andq %rcx, %rax
cmpq $0x0, %rax
je 0x42fc5
jmp 0x42c12
movq 0xa0(%rsp), %rax
movl 0x18(%rax), %edi
xorl %eax, %eax
movl %eax, %edx
movq %rdx, %rsi
callq 0x288b0
movl %eax, 0x29c(%rsp)
cmpl $-0x1, %eax
je 0x42e9e
movl 0x29c(%rsp), %edi
callq 0x42940
testb $0x1, %al
jne 0x42dc8
movb $0x1, 0xee(%rsp)
movl $0x48, %edi
callq 0x28e70
movq %rax, 0x68(%rsp)
leaq 0x117(%rsp), %rdi
movq %rdi, 0x70(%rsp)
callq 0x2b730
movq 0x70(%rsp), %rdx
leaq 0x3a5c(%rip), %rsi      # 0x466de
leaq 0x118(%rsp), %rdi
callq 0x2aa80
jmp 0x42c91
leaq 0xef(%rsp), %rdi
movq %rdi, 0x60(%rsp)
callq 0x2b730
movq 0x60(%rsp), %rdx
leaq 0x38c1(%rip), %rsi      # 0x46570
leaq 0xf0(%rsp), %rdi
callq 0x2aa80
jmp 0x42cbe
leaq 0x138(%rsp), %rdi
leaq 0x118(%rsp), %rsi
leaq 0xf0(%rsp), %rdx
callq 0x29b70
jmp 0x42cdd
movq 0x68(%rsp), %rdi
leaq 0x138(%rsp), %rsi
callq 0x2a7b0
jmp 0x42cf1
movq 0x68(%rsp), %rdi
movb $0x0, 0xee(%rsp)
movq 0x102c3(%rip), %rsi     # 0x52fc8
movq 0x1023c(%rip), %rdx     # 0x52f48
callq 0x2b540
jmp 0x4329e
movq %rax, %rcx
movl %edx, %eax
movq %rcx, 0x160(%rsp)
movl %eax, 0x15c(%rsp)
jmp 0x42da0
movq %rax, %rcx
movl %edx, %eax
movq %rcx, 0x160(%rsp)
movl %eax, 0x15c(%rsp)
jmp 0x42d86
movq %rax, %rcx
movl %edx, %eax
movq %rcx, 0x160(%rsp)
movl %eax, 0x15c(%rsp)
jmp 0x42d79
movq %rax, %rcx
movl %edx, %eax
movq %rcx, 0x160(%rsp)
movl %eax, 0x15c(%rsp)
leaq 0x138(%rsp), %rdi
callq 0x29360
leaq 0xf0(%rsp), %rdi
callq 0x29360
leaq 0xef(%rsp), %rdi
callq 0x2a210
leaq 0x118(%rsp), %rdi
callq 0x29360
leaq 0x117(%rsp), %rdi
callq 0x2a210
testb $0x1, 0xee(%rsp)
jne 0x42db9
jmp 0x42dc3
movq 0x68(%rsp), %rdi
callq 0x29690
jmp 0x43291
movq 0xa0(%rsp), %rdi
addq $0x20, %rdi
leaq 0x29c(%rsp), %rsi
callq 0x28320
movl 0x29c(%rsp), %eax
movl $0x40, %ecx
cltd
idivl %ecx
movl %edx, %eax
movl %eax, %ecx
movl $0x1, %eax
shlq %cl, %rax
movq 0xa0(%rsp), %rcx
movq %rax, 0x58(%rsp)
movl 0x29c(%rsp), %eax
movl $0x40, %esi
cltd
idivl %esi
movq 0x58(%rsp), %rdx
cltq
orq 0xf0(%rcx,%rax,8), %rdx
movq %rdx, 0xf0(%rcx,%rax,8)
movl 0x29c(%rsp), %eax
cmpl 0x170(%rcx), %eax
jle 0x42e4b
movl 0x29c(%rsp), %eax
movl %eax, 0x54(%rsp)
jmp 0x42e5d
movq 0xa0(%rsp), %rax
movl 0x170(%rax), %eax
movl %eax, 0x54(%rsp)
movq 0xa0(%rsp), %rdi
movl 0x54(%rsp), %eax
movl %eax, 0x170(%rdi)
addq $0x50, %rdi
callq 0x29b50
testb $0x1, %al
jne 0x42e7e
jmp 0x42e99
movq 0xa0(%rsp), %rsi
movq %rsi, %rdi
addq $0x50, %rdi
movl 0x29c(%rsp), %edx
callq 0x2a300
jmp 0x42c12
callq 0x280d0
cmpl $0xb, (%rax)
je 0x42fc3
callq 0x280d0
cmpl $0xb, (%rax)
je 0x42fc3
movb $0x1, 0xc6(%rsp)
movl $0x48, %edi
callq 0x28e70
movq %rax, 0x48(%rsp)
callq 0x42930
movl %eax, 0x50(%rsp)
jmp 0x42edc
movl 0x50(%rsp), %edi
callq 0x28970
movq %rax, 0x38(%rsp)
leaq 0xc7(%rsp), %rdi
movq %rdi, 0x40(%rsp)
callq 0x2b730
movq 0x38(%rsp), %rsi
movq 0x40(%rsp), %rdx
leaq 0xc8(%rsp), %rdi
callq 0x2aa80
jmp 0x42f15
movq 0x48(%rsp), %rdi
leaq 0xc8(%rsp), %rsi
callq 0x2a230
jmp 0x42f29
movq 0x48(%rsp), %rdi
movb $0x0, 0xc6(%rsp)
movq 0x10003(%rip), %rsi     # 0x52f40
movq 0x1000c(%rip), %rdx     # 0x52f50
callq 0x2b540
jmp 0x4329e
movq %rax, %rcx
movl %edx, %eax
movq %rcx, 0x160(%rsp)
movl %eax, 0x15c(%rsp)
jmp 0x42fa8
movq %rax, %rcx
movl %edx, %eax
movq %rcx, 0x160(%rsp)
movl %eax, 0x15c(%rsp)
jmp 0x42f9b
movq %rax, %rcx
movl %edx, %eax
movq %rcx, 0x160(%rsp)
movl %eax, 0x15c(%rsp)
leaq 0xc8(%rsp), %rdi
callq 0x29360
leaq 0xc7(%rsp), %rdi
callq 0x2a210
testb $0x1, 0xc6(%rsp)
jne 0x42fb4
jmp 0x42fbe
movq 0x48(%rsp), %rdi
callq 0x29690
jmp 0x43291
jmp 0x42fc5
movq 0xa0(%rsp), %rdi
addq $0x20, %rdi
callq 0x28c00
movq %rax, 0xb8(%rsp)
movq 0xa0(%rsp), %rdi
addq $0x20, %rdi
callq 0x296e0
movq %rax, 0xb0(%rsp)
leaq 0xb8(%rsp), %rdi
leaq 0xb0(%rsp), %rsi
callq 0x2b500
movb %al, %cl
xorl %eax, %eax
testb $0x1, %cl
movb %al, 0x37(%rsp)
jne 0x4301b
jmp 0x43037
movq 0xa0(%rsp), %rdi
addq $0x20, %rdi
callq 0x2be60
cmpq $0x0, %rax
setne %al
movb %al, 0x37(%rsp)
movb 0x37(%rsp), %al
testb $0x1, %al
jne 0x43044
jmp 0x43284
leaq 0xb8(%rsp), %rdi
callq 0x2ad70
movl (%rax), %eax
movl $0x40, %ecx
cltd
idivl %ecx
cltq
movq 0x218(%rsp,%rax,8), %rax
movq %rax, 0x28(%rsp)
leaq 0xb8(%rsp), %rdi
callq 0x2ad70
movl (%rax), %eax
movl $0x40, %ecx
cltd
idivl %ecx
movq 0x28(%rsp), %rax
movl %edx, %ecx
movl $0x1, %edx
shlq %cl, %rdx
movq %rdx, %rcx
andq %rcx, %rax
cmpq $0x0, %rax
je 0x431a8
leaq 0xb8(%rsp), %rdi
callq 0x2ad70
movq 0xa0(%rsp), %rcx
movl (%rax), %eax
cmpl 0x18(%rcx), %eax
jne 0x430c1
jmp 0x431a6
leaq 0xb8(%rsp), %rdi
callq 0x2ad70
movq 0xa0(%rsp), %rdi
movl (%rax), %esi
callq 0x2b7f0
cmpq $0x0, %rax
jne 0x4312d
leaq 0xb8(%rsp), %rdi
callq 0x2ad70
movq 0xa0(%rsp), %rdi
movl (%rax), %esi
callq 0x2aa00
movq 0xa0(%rsp), %rdi
addq $0x20, %rdi
callq 0x28c00
movq %rax, 0xa8(%rsp)
movq 0xa8(%rsp), %rax
movq %rax, 0xb8(%rsp)
jmp 0x43272
movq 0xa0(%rsp), %rdi
addq $0x90, %rdi
callq 0x29120
testb $0x1, %al
jne 0x43147
jmp 0x431a2
movq 0xa0(%rsp), %rax
addq $0x90, %rax
movq %rax, 0x18(%rsp)
leaq 0xb8(%rsp), %rdi
callq 0x2ad70
movl (%rax), %eax
movl %eax, 0x24(%rsp)
leaq 0xb8(%rsp), %rdi
callq 0x2ad70
movq 0xa0(%rsp), %rdi
movl (%rax), %esi
callq 0x2b7f0
movq 0x18(%rsp), %rdi
movq 0xa0(%rsp), %rsi
movl 0x24(%rsp), %edx
movq %rax, %rcx
callq 0x2aff0
jmp 0x431a4
jmp 0x431a6
jmp 0x431a8
leaq 0xb8(%rsp), %rdi
callq 0x2ad70
movl (%rax), %eax
movl $0x40, %ecx
cltd
idivl %ecx
cltq
movq 0x198(%rsp,%rax,8), %rax
movq %rax, 0x10(%rsp)
leaq 0xb8(%rsp), %rdi
callq 0x2ad70
movl (%rax), %eax
movl $0x40, %ecx
cltd
idivl %ecx
movq 0x10(%rsp), %rax
movl %edx, %ecx
movl $0x1, %edx
shlq %cl, %rdx
movq %rdx, %rcx
andq %rcx, %rax
cmpq $0x0, %rax
je 0x43270
leaq 0xb8(%rsp), %rdi
callq 0x2ad70
movq 0xa0(%rsp), %rcx
movl (%rax), %eax
cmpl 0x18(%rcx), %eax
jne 0x4321e
jmp 0x4326e
movq 0xa0(%rsp), %rdi
addq $0xb0, %rdi
callq 0x29b50
testb $0x1, %al
jne 0x43238
jmp 0x4326c
movq 0xa0(%rsp), %rax
addq $0xb0, %rax
movq %rax, 0x8(%rsp)
leaq 0xb8(%rsp), %rdi
callq 0x2ad70
movq 0x8(%rsp), %rdi
movq 0xa0(%rsp), %rsi
movl (%rax), %edx
callq 0x2a300
jmp 0x4326e
jmp 0x43270
jmp 0x43272
leaq 0xb8(%rsp), %rdi
callq 0x29810
jmp 0x42fde
jmp 0x429bf
addq $0x2a8, %rsp            # imm = 0x2A8
retq
movq 0x160(%rsp), %rdi
callq 0x2b6f0
nop