NetServer::NetServer(WORD port, int nthreads)
   : sock(true), pool(0), conn(0), poolsize(nthreads), err(0),
     server_shutdown(false), hreader(0)
{
   NetHost host;
   addr = NetAddr(host.Address().IPAddr(), port);

   sock.bind(addr);
   sock.listen(3);

   if (poolsize < 1) poolsize = 1;

   pool = new(__FILE__,__LINE__) HANDLE[poolsize];
   conn = new(__FILE__,__LINE__) NetSock*[poolsize];
   clients = new(__FILE__,__LINE__) NetAddr[poolsize];

   if (pool && conn && clients) {
      ZeroMemory(pool, poolsize * sizeof(HANDLE));
      ZeroMemory(conn, poolsize * sizeof(NetSock*));

      DWORD thread_id = 0;

      for (int i = 0; i < poolsize; i++) {
         thread_id = 0;
         PoolItem* item = new PoolItem;
         item->server = this;
         item->thread_index = i;

         pool[i] = CreateThread(0, 4096, NetServerReaderProc, (LPVOID) item, 0, &thread_id);
      }

      thread_id = 0;
      hreader = CreateThread(0, 4096, NetServerListenerProc, (LPVOID) this, 0, &thread_id);
   }
}
NetGameClient::NetGameClient()
: server_id(0), join_req_time(0)
{
	Print("Constructing NetGameClient\n");

	NetHost me;
	Text    server_name;
	WORD    port = 11101;

	NetClientConfig* ncc = NetClientConfig::GetInstance();
	if (ncc) {
		NetServerInfo* info = ncc->GetSelectedServer();

		if (info) {
			server_name = info->hostname;
			port        = info->gameport;
		}
	}

	if (server_name.length() && port > 0) {
		Print("  '%s' is a client of '%s'\n", me.Name(), server_name);
		link = new(__FILE__,__LINE__) NetLink;
		server_id = link->AddPeer(NetAddr(server_name, port));
		SendJoinRequest();
	}
	else if (port == 0) {
		Print("  '%s' invalid game port number %d\n", me.Name(), port);
	}
	else {
		Print("  '%s' is a client without a server\n", me.Name());
	}
}
NetLobbyClient::NetLobbyClient()
: NetLobby(false), server_id(0), host(false), exit_code(0), temporary(false)
{
    NetHost me;
    Text    server_name;
    WORD    port = 11101;

    ping_req_time = 0;
    chat_req_time = 0;
    user_req_time = 0;
    camp_req_time = 0;
    unit_req_time = 0;
    mods_req_time = 0;

    NetClientConfig* ncc = NetClientConfig::GetInstance();
    if (ncc) {
        NetServerInfo* info = ncc->GetSelectedServer();

        if (info) {
            server_name = info->hostname;
            addr        = info->addr;
            port        = info->port;
            gamepass    = info->password;
        }
    }

    if (server_name.length() && port > 0) {
        Print("  '%s' is a client of '%s'\n", me.Name(), server_name.data());
        link = new(__FILE__,__LINE__) NetLink;
        server_id = link->AddPeer(NetAddr(server_name, port));
    }
    else if (port == 0) {
        Print("  '%s' invalid lobby port number %d\n", me.Name(), port);
    }
    else {
        Print("  '%s' is a client without a server\n", me.Name());
    }
}