]> de.git.xonotic.org Git - xonotic/darkplaces.git/blobdiff - netconn.c
qw support is 99% working
[xonotic/darkplaces.git] / netconn.c
index 4e68c72a15ffc8013de416b27d789937341f6085..66129781f8c69c305d018c264ce3ad9443f103e5 100755 (executable)
--- a/netconn.c
+++ b/netconn.c
@@ -25,36 +25,41 @@ Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
 
 #define MASTER_PORT 27950
 
-cvar_t sv_public = {0, "sv_public", "1"};
-static cvar_t sv_heartbeatperiod = {CVAR_SAVE, "sv_heartbeatperiod", "180"};
+// note this defaults on for dedicated servers, off for listen servers
+cvar_t sv_public = {0, "sv_public", "0", "advertises this server on the master server (so that players can find it in the server browser)"};
+static cvar_t sv_heartbeatperiod = {CVAR_SAVE, "sv_heartbeatperiod", "120", "how often to send heartbeat in seconds (only used if sv_public is 1)"};
 
 // FIXME: resolve DNS on masters whenever their value changes and cache it (to avoid major delays in active servers when they heartbeat)
 static cvar_t sv_masters [] =
 {
-       {CVAR_SAVE, "sv_master1", ""},
-       {CVAR_SAVE, "sv_master2", ""},
-       {CVAR_SAVE, "sv_master3", ""},
-       {CVAR_SAVE, "sv_master4", ""},
-       {0, "sv_masterextra1", "69.59.212.88"}, // ghdigital.com
-       {0, "sv_masterextra2", "66.169.205.13"}, // dpmaster.deathmask.net
-       {0, "sv_masterextra3", "12.166.196.192"}, // blaze.mindphukd.org
-       {0, NULL, NULL}
+       {CVAR_SAVE, "sv_master1", "", "user-chosen master server 1"},
+       {CVAR_SAVE, "sv_master2", "", "user-chosen master server 2"},
+       {CVAR_SAVE, "sv_master3", "", "user-chosen master server 3"},
+       {CVAR_SAVE, "sv_master4", "", "user-chosen master server 4"},
+       {0, "sv_masterextra1", "ghdigital.com", "default master server 1 (admin: LordHavoc)"}, // admin: LordHavoc
+       {0, "sv_masterextra2", "dpmaster.deathmask.net", "default master server 2 (admin: Willis)"}, // admin: Willis
+       {0, "sv_masterextra3", "12.166.196.192", "default master server 3 (admin: Venim)"}, // admin: Venim
+       {0, "sv_masterextra4", "excalibur.nvg.ntnu.no", "default master server 4 (admin: tChr)"}, // admin: tChr
+       {0, NULL, NULL, NULL}
 };
 
 static double nextheartbeattime = 0;
 
 sizebuf_t net_message;
-static qbyte net_message_buf[NET_MAXMESSAGE];
+static unsigned char net_message_buf[NET_MAXMESSAGE];
 
-cvar_t net_messagetimeout = {0, "net_messagetimeout","300"};
-cvar_t net_messagerejointimeout = {0, "net_messagerejointimeout","10"};
-cvar_t net_connecttimeout = {0, "net_connecttimeout","10"};
-cvar_t hostname = {CVAR_SAVE, "hostname", "UNNAMED"};
-cvar_t developer_networking = {0, "developer_networking", "0"};
-
-cvar_t cl_netlocalping = {0, "cl_netlocalping","0"};
-static cvar_t cl_netpacketloss = {0, "cl_netpacketloss","0"};
+cvar_t net_messagetimeout = {0, "net_messagetimeout","300", "drops players who have not sent any packets for this many seconds"};
+cvar_t net_messagerejointimeout = {0, "net_messagerejointimeout","10", "give a player this much time in seconds to rejoin and continue playing (not losing frags and such)"};
+cvar_t net_connecttimeout = {0, "net_connecttimeout","10", "after requesting a connection, the client must reply within this many seconds or be dropped (cuts down on connect floods)"};
+cvar_t hostname = {CVAR_SAVE, "hostname", "UNNAMED", "server message to show in server browser"};
+cvar_t developer_networking = {0, "developer_networking", "0", "prints all received and sent packets (recommended only for debugging)"};
 
+cvar_t cl_netlocalping = {0, "cl_netlocalping","0", "lags local loopback connection by this much ping time (useful to play more fairly on your own server with people with higher pings)"};
+static cvar_t cl_netpacketloss = {0, "cl_netpacketloss","0", "drops this percentage of packets (incoming and outgoing), useful for testing network protocol robustness (effects failing to start, sounds failing to play, etc)"};
+static cvar_t net_slist_queriespersecond = {0, "net_slist_queriespersecond", "20", "how many server information requests to send per second"};
+static cvar_t net_slist_queriesperframe = {0, "net_slist_queriesperframe", "4", "maximum number of server information requests to send each rendered frame (guards against low framerates causing problems)"};
+static cvar_t net_slist_timeout = {0, "net_slist_timeout", "4", "how long to listen for a server information response before giving up"};
+static cvar_t net_slist_maxtries = {0, "net_slist_maxtries", "3", "how many times to ask the same server for information (more times gives better ping reports but takes longer)"};
 
 /* statistic counters */
 static int packetsSent = 0;
@@ -74,8 +79,11 @@ int masterreplycount = 0;
 int serverquerycount = 0;
 int serverreplycount = 0;
 
-static qbyte sendbuffer[NET_HEADERSIZE+NET_MAXMESSAGE];
-static qbyte readbuffer[NET_HEADERSIZE+NET_MAXMESSAGE];
+// this is only false if there are still servers left to query
+int serverlist_querysleep = true;
+
+static unsigned char sendbuffer[NET_HEADERSIZE+NET_MAXMESSAGE];
+static unsigned char readbuffer[NET_HEADERSIZE+NET_MAXMESSAGE];
 
 int cl_numsockets;
 lhnetsocket_t *cl_sockets[16];
@@ -85,10 +93,10 @@ lhnetsocket_t *sv_sockets[16];
 netconn_t *netconn_list = NULL;
 mempool_t *netconn_mempool = NULL;
 
-cvar_t cl_netport = {0, "cl_port", "0"};
-cvar_t sv_netport = {0, "port", "26000"};
-cvar_t net_address = {0, "net_address", "0.0.0.0"};
-//cvar_t net_netaddress_ipv6 = {0, "net_address_ipv6", "[0:0:0:0:0:0:0:0]"};
+cvar_t cl_netport = {0, "cl_port", "0", "forces client to use chosen port number if not 0"};
+cvar_t sv_netport = {0, "port", "26000", "server port for players to connect to"};
+cvar_t net_address = {0, "net_address", "0.0.0.0", "network address to open ports on"};
+//cvar_t net_netaddress_ipv6 = {0, "net_address_ipv6", "[0:0:0:0:0:0:0:0]", "network address to open ipv6 ports on"};
 
 // ServerList interface
 serverlist_mask_t serverlist_andmasks[SERVERLIST_ANDMASKCOUNT];
@@ -110,14 +118,16 @@ qboolean serverlist_consoleoutput;
 static void _ServerList_ViewList_Helper_InsertBefore( int index, serverlist_entry_t *entry )
 {
     int i;
-       if( serverlist_viewcount == SERVERLIST_VIEWLISTSIZE )
-               return;
+       if( serverlist_viewcount < SERVERLIST_VIEWLISTSIZE ) {
+               i = serverlist_viewcount++;
+       } else {
+               i = SERVERLIST_VIEWLISTSIZE - 1;
+       }
 
-       for( i = serverlist_viewcount ; i > index ; i-- )
+       for( ; i > index ; i-- )
                serverlist_viewlist[ i ] = serverlist_viewlist[ i - 1 ];
 
        serverlist_viewlist[index] = entry;
-       serverlist_viewcount++;
 }
 
 // we suppose serverlist_viewcount to be valid, ie > 0
@@ -197,25 +207,34 @@ static qboolean _ServerList_CompareInt( int A, serverlist_maskop_t op, int B )
 
 static qboolean _ServerList_CompareStr( const char *A, serverlist_maskop_t op, const char *B )
 {
+       int i;
+       char bufferA[ 256 ], bufferB[ 256 ]; // should be more than enough
+       for (i = 0;i < (int)sizeof(bufferA)-1 && A[i];i++)
+               bufferA[i] = (A[i] >= 'A' && A[i] <= 'Z') ? (A[i] + 'a' - 'A') : A[i];
+       bufferA[i] = 0;
+       for (i = 0;i < (int)sizeof(bufferB)-1 && B[i];i++)
+               bufferB[i] = (B[i] >= 'A' && B[i] <= 'Z') ? (B[i] + 'a' - 'A') : B[i];
+       bufferB[i] = 0;
+
        // Same here, also using an intermediate & final return would be more appropriate
        // A info B mask
        switch( op ) {
                case SLMO_CONTAINS:
-                       return *B && !!strstr( A, B ); // we want a real bool
+                       return *bufferB && !!strstr( bufferA, bufferB ); // we want a real bool
                case SLMO_NOTCONTAIN:
-                       return !*B || !strstr( A, B );
+                       return !*bufferB || !strstr( bufferA, bufferB );
                case SLMO_LESS:
-                       return strcmp( A, B ) < 0;
+                       return strcmp( bufferA, bufferB ) < 0;
                case SLMO_LESSEQUAL:
-                       return strcmp( A, B ) <= 0;
+                       return strcmp( bufferA, bufferB ) <= 0;
                case SLMO_EQUAL:
-                       return strcmp( A, B ) == 0;
+                       return strcmp( bufferA, bufferB ) == 0;
                case SLMO_GREATER:
-                       return strcmp( A, B ) > 0;
+                       return strcmp( bufferA, bufferB ) > 0;
                case SLMO_NOTEQUAL:
-                       return strcmp( A, B ) != 0;
+                       return strcmp( bufferA, bufferB ) != 0;
                case SLMO_GREATEREQUAL:
-                       return strcmp( A, B ) >= 0;
+                       return strcmp( bufferA, bufferB ) >= 0;
                default:
                        Con_DPrint( "_ServerList_CompareStr: Bad op!\n" );
                        return false;
@@ -254,9 +273,7 @@ static void ServerList_ViewList_Insert( serverlist_entry_t *entry )
 {
        int start, end, mid;
 
-       if( serverlist_viewcount == SERVERLIST_VIEWLISTSIZE )
-               return;
-
+       // FIXME: change this to be more readable (...)
        // now check whether it passes through the masks
        for( start = 0 ; serverlist_andmasks[start].active && start < SERVERLIST_ANDMASKCOUNT ; start++ )
                if( !_ServerList_Entry_Mask( &serverlist_andmasks[start], &entry->info ) )
@@ -319,7 +336,7 @@ void ServerList_RebuildViewList(void)
 
        serverlist_viewcount = 0;
        for( i = 0 ; i < serverlist_cachecount ; i++ )
-               if( serverlist_cache[i].finished )
+               if( serverlist_cache[i].query == SQS_QUERIED )
                        ServerList_ViewList_Insert( &serverlist_cache[i] );
 }
 
@@ -335,8 +352,8 @@ static void _ServerList_Test(void)
        int i;
        for( i = 0 ; i < 1024 ; i++ ) {
                memset( &serverlist_cache[serverlist_cachecount], 0, sizeof( serverlist_entry_t ) );
-               serverlist_cache[serverlist_cachecount].info.ping = rand() % 450 + 250;
-               dpsnprintf( serverlist_cache[serverlist_cachecount].info.name, 128, "Black's ServerList Test %i", i );
+               serverlist_cache[serverlist_cachecount].info.ping = 1000 + 1024 - i;
+               dpsnprintf( serverlist_cache[serverlist_cachecount].info.name, sizeof(serverlist_cache[serverlist_cachecount].info.name), "Black's ServerList Test %i", i );
                serverlist_cache[serverlist_cachecount].finished = true;
                sprintf( serverlist_cache[serverlist_cachecount].line1, "%i %s", serverlist_cache[serverlist_cachecount].info.ping, serverlist_cache[serverlist_cachecount].info.name );
                ServerList_ViewList_Insert( &serverlist_cache[serverlist_cachecount] );
@@ -355,9 +372,10 @@ void ServerList_QueryList(void)
        serverlist_cachecount = 0;
        serverlist_viewcount = 0;
        serverlist_consoleoutput = false;
-       NetConn_QueryMasters();
 
        //_ServerList_Test();
+
+       NetConn_QueryMasters();
 }
 
 // rest
@@ -380,7 +398,7 @@ int NetConn_Read(lhnetsocket_t *mysocket, void *data, int maxlength, lhnetaddres
                {
                        LHNETADDRESS_ToString(peeraddress, addressstring2, sizeof(addressstring2), true);
                        Con_Printf("LHNET_Read(%p (%s), %p, %i, %p) = %i from %s:\n", mysocket, addressstring, data, maxlength, peeraddress, length, addressstring2);
-                       Com_HexDumpToConsole(data, length);
+                       Com_HexDumpToConsole((unsigned char *)data, length);
                }
                else
                        Con_Printf("LHNET_Read(%p (%s), %p, %i, %p) = %i\n", mysocket, addressstring, data, maxlength, peeraddress, length);
@@ -403,7 +421,7 @@ int NetConn_Write(lhnetsocket_t *mysocket, const void *data, int length, const l
                LHNETADDRESS_ToString(LHNET_AddressFromSocket(mysocket), addressstring, sizeof(addressstring), true);
                LHNETADDRESS_ToString(peeraddress, addressstring2, sizeof(addressstring2), true);
                Con_Printf("LHNET_Write(%p (%s), %p, %i, %p (%s)) = %i%s\n", mysocket, addressstring, data, length, peeraddress, addressstring2, length, ret == length ? "" : " (ERROR)");
-               Com_HexDumpToConsole(data, length);
+               Com_HexDumpToConsole((unsigned char *)data, length);
        }
        return ret;
 }
@@ -411,168 +429,180 @@ int NetConn_Write(lhnetsocket_t *mysocket, const void *data, int length, const l
 int NetConn_WriteString(lhnetsocket_t *mysocket, const char *string, const lhnetaddress_t *peeraddress)
 {
        // note this does not include the trailing NULL because we add that in the parser
-       return NetConn_Write(mysocket, string, strlen(string), peeraddress);
-}
-
-int NetConn_SendReliableMessage(netconn_t *conn, sizebuf_t *data)
-{
-       unsigned int packetLen;
-       unsigned int dataLen;
-       unsigned int eom;
-       unsigned int *header;
-
-//#ifdef DEBUG
-       if (data->cursize == 0)
-               Sys_Error("Datagram_SendMessage: zero length message\n");
-
-       if (data->cursize > (int)sizeof(conn->sendMessage))
-               Sys_Error("Datagram_SendMessage: message too big (%u > %u)\n", data->cursize, sizeof(conn->sendMessage));
-
-       if (conn->canSend == false)
-               Sys_Error("SendMessage: called with canSend == false\n");
-//#endif
-
-       memcpy(conn->sendMessage, data->data, data->cursize);
-       conn->sendMessageLength = data->cursize;
-
-       if (conn->sendMessageLength <= MAX_PACKETFRAGMENT)
-       {
-               dataLen = conn->sendMessageLength;
-               eom = NETFLAG_EOM;
-       }
-       else
-       {
-               dataLen = MAX_PACKETFRAGMENT;
-               eom = 0;
-       }
-
-       packetLen = NET_HEADERSIZE + dataLen;
-
-       header = (void *)sendbuffer;
-       header[0] = BigLong(packetLen | (NETFLAG_DATA | eom));
-       header[1] = BigLong(conn->sendSequence);
-       memcpy(sendbuffer + NET_HEADERSIZE, conn->sendMessage, dataLen);
-
-       conn->sendSequence++;
-       conn->canSend = false;
-
-       if (NetConn_Write(conn->mysocket, (void *)&sendbuffer, packetLen, &conn->peeraddress) != (int)packetLen)
-               return -1;
-
-       conn->lastSendTime = realtime;
-       packetsSent++;
-       reliableMessagesSent++;
-       return 1;
+       return NetConn_Write(mysocket, string, (int)strlen(string), peeraddress);
 }
 
-static void NetConn_SendMessageNext(netconn_t *conn)
+int NetConn_SendUnreliableMessage(netconn_t *conn, sizebuf_t *data, protocolversion_t protocol)
 {
-       unsigned int packetLen;
-       unsigned int dataLen;
-       unsigned int eom;
-       unsigned int *header;
-
-       if (conn->sendMessageLength && !conn->canSend && conn->sendNext)
+       if (protocol == PROTOCOL_QUAKEWORLD)
        {
-               if (conn->sendMessageLength <= MAX_PACKETFRAGMENT)
+               int packetLen;
+               qboolean sendreliable;
+
+               // note that it is ok to send empty messages to the qw server,
+               // otherwise it won't respond to us at all
+
+               sendreliable = false;
+               // if the remote side dropped the last reliable message, resend it
+               if (conn->qw.incoming_acknowledged > conn->qw.last_reliable_sequence && conn->qw.incoming_reliable_acknowledged != conn->qw.reliable_sequence)
+                       sendreliable = true;
+               // if the reliable transmit buffer is empty, copy the current message out
+               if (!conn->sendMessageLength && conn->message.cursize)
                {
-                       dataLen = conn->sendMessageLength;
-                       eom = NETFLAG_EOM;
+                       memcpy (conn->sendMessage, conn->message.data, conn->message.cursize);
+                       conn->sendMessageLength = conn->message.cursize;
+                       SZ_Clear(&conn->message); // clear the message buffer
+                       conn->qw.reliable_sequence ^= 1;
+                       sendreliable = true;
                }
-               else
+               // outgoing unreliable packet number, and outgoing reliable packet number (0 or 1)
+               *((int *)(sendbuffer + 0)) = LittleLong((unsigned int)conn->qw.outgoing_sequence | ((unsigned int)sendreliable<<31));
+               // last received unreliable packet number, and last received reliable packet number (0 or 1)
+               *((int *)(sendbuffer + 4)) = LittleLong((unsigned int)conn->qw.incoming_sequence | ((unsigned int)conn->qw.incoming_reliable_sequence<<31));
+               packetLen = 8;
+               conn->qw.outgoing_sequence++;
+               // client sends qport in every packet
+               if (conn == cls.netcon)
                {
-                       dataLen = MAX_PACKETFRAGMENT;
-                       eom = 0;
+                       *((short *)(sendbuffer + 8)) = LittleShort(cls.qw_qport);
+                       packetLen += 2;
+               }
+               if (packetLen + (sendreliable ? conn->sendMessageLength : 0) > 1400)
+               {
+                       Con_Printf ("NetConn_SendUnreliableMessage: reliable message too big %u\n", data->cursize);
+                       return -1;
+               }
+               // add the reliable message if there is one
+               if (sendreliable)
+               {
+                       memcpy(sendbuffer + packetLen, conn->sendMessage, conn->sendMessageLength);
+                       packetLen += conn->sendMessageLength;
+                       conn->qw.last_reliable_sequence = conn->qw.outgoing_sequence;
+               }
+               // add the unreliable message if possible
+               if (packetLen + data->cursize <= 1400)
+               {
+                       memcpy(sendbuffer + packetLen, data->data, data->cursize);
+                       packetLen += data->cursize;
                }
 
-               packetLen = NET_HEADERSIZE + dataLen;
-
-               header = (void *)sendbuffer;
-               header[0] = BigLong(packetLen | (NETFLAG_DATA | eom));
-               header[1] = BigLong(conn->sendSequence);
-               memcpy(sendbuffer + NET_HEADERSIZE, conn->sendMessage, dataLen);
-
-               conn->sendSequence++;
-               conn->sendNext = false;
-
-               if (NetConn_Write(conn->mysocket, (void *)&sendbuffer, packetLen, &conn->peeraddress) != (int)packetLen)
-                       return;
+               NetConn_Write(conn->mysocket, (void *)&sendbuffer, packetLen, &conn->peeraddress);
 
-               conn->lastSendTime = realtime;
                packetsSent++;
-       }
-}
+               unreliableMessagesSent++;
 
-static void NetConn_ReSendMessage(netconn_t *conn)
-{
-       unsigned int packetLen;
-       unsigned int dataLen;
-       unsigned int eom;
-       unsigned int *header;
+               // delay later packets to obey rate limit
+               conn->qw.cleartime = max(conn->qw.cleartime, realtime) + packetLen * conn->qw.rate;
 
-       if (conn->sendMessageLength && !conn->canSend && (realtime - conn->lastSendTime) > 1.0)
+               return 0;
+       }
+       else
        {
-               if (conn->sendMessageLength <= MAX_PACKETFRAGMENT)
+               unsigned int packetLen;
+               unsigned int dataLen;
+               unsigned int eom;
+               unsigned int *header;
+
+               // if a reliable message fragment has been lost, send it again
+               if (conn->sendMessageLength && (realtime - conn->lastSendTime) > 1.0)
                {
-                       dataLen = conn->sendMessageLength;
-                       eom = NETFLAG_EOM;
+                       if (conn->sendMessageLength <= MAX_PACKETFRAGMENT)
+                       {
+                               dataLen = conn->sendMessageLength;
+                               eom = NETFLAG_EOM;
+                       }
+                       else
+                       {
+                               dataLen = MAX_PACKETFRAGMENT;
+                               eom = 0;
+                       }
+
+                       packetLen = NET_HEADERSIZE + dataLen;
+
+                       header = (unsigned int *)sendbuffer;
+                       header[0] = BigLong(packetLen | (NETFLAG_DATA | eom));
+                       header[1] = BigLong(conn->nq.sendSequence - 1);
+                       memcpy(sendbuffer + NET_HEADERSIZE, conn->sendMessage, dataLen);
+
+                       if (NetConn_Write(conn->mysocket, (void *)&sendbuffer, packetLen, &conn->peeraddress) == (int)packetLen)
+                       {
+                               conn->lastSendTime = realtime;
+                               packetsReSent++;
+                       }
                }
-               else
+
+               // if we have a new reliable message to send, do so
+               if (!conn->sendMessageLength && conn->message.cursize)
                {
-                       dataLen = MAX_PACKETFRAGMENT;
-                       eom = 0;
-               }
+                       if (conn->message.cursize > (int)sizeof(conn->sendMessage))
+                       {
+                               Con_Printf("NetConn_SendUnreliableMessage: reliable message too big (%u > %u)\n", conn->message.cursize, sizeof(conn->sendMessage));
+                               conn->message.overflowed = true;
+                               return -1;
+                       }
 
-               packetLen = NET_HEADERSIZE + dataLen;
+                       if (developer_networking.integer && conn == cls.netcon)
+                       {
+                               Con_Print("client sending reliable message to server:\n");
+                               SZ_HexDumpToConsole(&conn->message);
+                       }
 
-               header = (void *)sendbuffer;
-               header[0] = BigLong(packetLen | (NETFLAG_DATA | eom));
-               header[1] = BigLong(conn->sendSequence - 1);
-               memcpy(sendbuffer + NET_HEADERSIZE, conn->sendMessage, dataLen);
+                       memcpy(conn->sendMessage, conn->message.data, conn->message.cursize);
+                       conn->sendMessageLength = conn->message.cursize;
+                       SZ_Clear(&conn->message);
 
-               conn->sendNext = false;
+                       if (conn->sendMessageLength <= MAX_PACKETFRAGMENT)
+                       {
+                               dataLen = conn->sendMessageLength;
+                               eom = NETFLAG_EOM;
+                       }
+                       else
+                       {
+                               dataLen = MAX_PACKETFRAGMENT;
+                               eom = 0;
+                       }
 
-               if (NetConn_Write(conn->mysocket, (void *)&sendbuffer, packetLen, &conn->peeraddress) != (int)packetLen)
-                       return;
+                       packetLen = NET_HEADERSIZE + dataLen;
 
-               conn->lastSendTime = realtime;
-               packetsReSent++;
-       }
-}
+                       header = (unsigned int *)sendbuffer;
+                       header[0] = BigLong(packetLen | (NETFLAG_DATA | eom));
+                       header[1] = BigLong(conn->nq.sendSequence);
+                       memcpy(sendbuffer + NET_HEADERSIZE, conn->sendMessage, dataLen);
 
-qboolean NetConn_CanSendMessage(netconn_t *conn)
-{
-       return conn->canSend;
-}
+                       conn->nq.sendSequence++;
 
-int NetConn_SendUnreliableMessage(netconn_t *conn, sizebuf_t *data)
-{
-       int packetLen;
-       int *header;
+                       NetConn_Write(conn->mysocket, (void *)&sendbuffer, packetLen, &conn->peeraddress);
 
-       packetLen = NET_HEADERSIZE + data->cursize;
+                       conn->lastSendTime = realtime;
+                       packetsSent++;
+                       reliableMessagesSent++;
+               }
 
-//#ifdef DEBUG
-       if (data->cursize == 0)
-               Sys_Error("Datagram_SendUnreliableMessage: zero length message\n");
+               // if we have an unreliable message to send, do so
+               if (data->cursize)
+               {
+                       packetLen = NET_HEADERSIZE + data->cursize;
 
-       if (packetLen > (int)sizeof(sendbuffer))
-               Sys_Error("Datagram_SendUnreliableMessage: message too big %u\n", data->cursize);
-//#endif
+                       if (packetLen > (int)sizeof(sendbuffer))
+                       {
+                               Con_Printf("NetConn_SendUnreliableMessage: message too big %u\n", data->cursize);
+                               return -1;
+                       }
 
-       header = (void *)sendbuffer;
-       header[0] = BigLong(packetLen | NETFLAG_UNRELIABLE);
-       header[1] = BigLong(conn->unreliableSendSequence);
-       memcpy(sendbuffer + NET_HEADERSIZE, data->data, data->cursize);
+                       header = (unsigned int *)sendbuffer;
+                       header[0] = BigLong(packetLen | NETFLAG_UNRELIABLE);
+                       header[1] = BigLong(conn->nq.unreliableSendSequence);
+                       memcpy(sendbuffer + NET_HEADERSIZE, data->data, data->cursize);
 
-       conn->unreliableSendSequence++;
+                       conn->nq.unreliableSendSequence++;
 
-       if (NetConn_Write(conn->mysocket, (void *)&sendbuffer, packetLen, &conn->peeraddress) != (int)packetLen)
-               return -1;
+                       NetConn_Write(conn->mysocket, (void *)&sendbuffer, packetLen, &conn->peeraddress);
 
-       packetsSent++;
-       unreliableMessagesSent++;
-       return 1;
+                       packetsSent++;
+                       unreliableMessagesSent++;
+               }
+               return 0;
+       }
 }
 
 void NetConn_CloseClientPorts(void)
@@ -629,29 +659,40 @@ void NetConn_OpenServerPort(const char *addressstring, int defaultport)
 {
        lhnetaddress_t address;
        lhnetsocket_t *s;
+       int port;
        char addressstring2[1024];
-       if (LHNETADDRESS_FromString(&address, addressstring, defaultport))
+
+       for (port = defaultport; port <= defaultport + 100; port++)
        {
-               if ((s = LHNET_OpenSocket_Connectionless(&address)))
+               if (LHNETADDRESS_FromString(&address, addressstring, port))
                {
-                       sv_sockets[sv_numsockets++] = s;
-                       LHNETADDRESS_ToString(LHNET_AddressFromSocket(s), addressstring2, sizeof(addressstring2), true);
-                       Con_Printf("Server listening on address %s\n", addressstring2);
+                       if ((s = LHNET_OpenSocket_Connectionless(&address)))
+                       {
+                               sv_sockets[sv_numsockets++] = s;
+                               LHNETADDRESS_ToString(LHNET_AddressFromSocket(s), addressstring2, sizeof(addressstring2), true);
+                               Con_Printf("Server listening on address %s\n", addressstring2);
+                               break;
+                       }
+                       else
+                       {
+                               LHNETADDRESS_ToString(&address, addressstring2, sizeof(addressstring2), true);
+                               Con_Printf("Server failed to open socket on address %s\n", addressstring2);
+                       }
                }
                else
                {
-                       LHNETADDRESS_ToString(&address, addressstring2, sizeof(addressstring2), true);
-                       Con_Printf("Server failed to open socket on address %s\n", addressstring2);
+                       Con_Printf("Server unable to parse address %s\n", addressstring);
+                       // if it cant parse one address, it wont be able to parse another for sure
+                       break;
                }
        }
-       else
-               Con_Printf("Server unable to parse address %s\n", addressstring);
 }
 
 void NetConn_OpenServerPorts(int opennetports)
 {
        int port;
        NetConn_CloseServerPorts();
+       NetConn_UpdateSockets();
        port = bound(0, sv_netport.integer, 65535);
        if (port == 0)
                port = 26000;
@@ -666,7 +707,7 @@ void NetConn_OpenServerPorts(int opennetports)
                //NetConn_OpenServerPort(net_address_ipv6.string, port);
        }
        if (sv_numsockets == 0)
-               Host_Error("NetConn_OpenServerPorts: unable to open any ports!\n");
+               Host_Error("NetConn_OpenServerPorts: unable to open any ports!");
 }
 
 lhnetsocket_t *NetConn_ChooseClientSocketForAddress(lhnetaddress_t *address)
@@ -690,11 +731,13 @@ lhnetsocket_t *NetConn_ChooseServerSocketForAddress(lhnetaddress_t *address)
 netconn_t *NetConn_Open(lhnetsocket_t *mysocket, lhnetaddress_t *peeraddress)
 {
        netconn_t *conn;
-       conn = Mem_Alloc(netconn_mempool, sizeof(*conn));
+       conn = (netconn_t *)Mem_Alloc(netconn_mempool, sizeof(*conn));
        conn->mysocket = mysocket;
        conn->peeraddress = *peeraddress;
-       conn->canSend = true;
        conn->lastMessageTime = realtime;
+       conn->message.data = conn->messagedata;
+       conn->message.maxsize = sizeof(conn->messagedata);
+       conn->message.cursize = 0;
        // LordHavoc: (inspired by ProQuake) use a short connect timeout to
        // reduce effectiveness of connection request floods
        conn->timeout = realtime + net_connecttimeout.value;
@@ -731,7 +774,7 @@ void NetConn_Close(netconn_t *conn)
 static int clientport = -1;
 static int clientport2 = -1;
 static int hostport = -1;
-static void NetConn_UpdateServerStuff(void)
+void NetConn_UpdateSockets(void)
 {
        if (cls.state != ca_dedicated)
        {
@@ -758,19 +801,81 @@ static void NetConn_UpdateServerStuff(void)
        }
 }
 
-int NetConn_ReceivedMessage(netconn_t *conn, qbyte *data, int length)
+static int NetConn_ReceivedMessage(netconn_t *conn, unsigned char *data, int length, protocolversion_t protocol)
 {
-       unsigned int count;
-       unsigned int flags;
-       unsigned int sequence;
+       if (length < 8)
+               return 0;
+
+       if (protocol == PROTOCOL_QUAKEWORLD)
+       {
+               int sequence, sequence_ack;
+               int reliable_ack, reliable_message;
+               int count;
+               int qport;
+
+               sequence = LittleLong(*((int *)(data + 0)));
+               sequence_ack = LittleLong(*((int *)(data + 4)));
+               data += 8;
+               length -= 8;
+
+               if (conn != cls.netcon)
+               {
+                       // server only
+                       if (length < 2)
+                               return 0;
+                       // TODO: use qport to identify that this client really is who they say they are?  (and elsewhere in the code to identify the connection without a port match?)
+                       qport = LittleShort(*((int *)(data + 8)));
+                       data += 2;
+                       length -= 2;
+               }
 
-       if (length >= 8)
+               packetsReceived++;
+               reliable_message = (sequence >> 31) & 1;
+               reliable_ack = (sequence_ack >> 31) & 1;
+               sequence &= ~(1<<31);
+               sequence_ack &= ~(1<<31);
+               if (sequence <= conn->qw.incoming_sequence)
+               {
+                       Con_DPrint("Got a stale datagram\n");
+                       return 0;
+               }
+               count = sequence - (conn->qw.incoming_sequence + 1);
+               if (count > 0)
+               {
+                       droppedDatagrams += count;
+                       Con_DPrintf("Dropped %u datagram(s)\n", count);
+               }
+               if (reliable_ack == conn->qw.reliable_sequence)
+               {
+                       // received, now we will be able to send another reliable message
+                       conn->sendMessageLength = 0;
+                       reliableMessagesReceived++;
+               }
+               conn->qw.incoming_sequence = sequence;
+               conn->qw.incoming_acknowledged = sequence_ack;
+               conn->qw.incoming_reliable_acknowledged = reliable_ack;
+               if (reliable_message)
+                       conn->qw.incoming_reliable_sequence ^= 1;
+               conn->lastMessageTime = realtime;
+               conn->timeout = realtime + net_messagetimeout.value;
+               unreliableMessagesReceived++;
+               SZ_Clear(&net_message);
+               SZ_Write(&net_message, data, length);
+               MSG_BeginReading();
+               return 2;
+       }
+       else
        {
-               length = BigLong(((int *)data)[0]);
-               flags = length & ~NETFLAG_LENGTH_MASK;
-               length &= NETFLAG_LENGTH_MASK;
+               unsigned int count;
+               unsigned int flags;
+               unsigned int sequence;
+               int qlength;
+
+               qlength = (unsigned int)BigLong(((int *)data)[0]);
+               flags = qlength & ~NETFLAG_LENGTH_MASK;
+               qlength &= NETFLAG_LENGTH_MASK;
                // control packets were already handled
-               if (!(flags & NETFLAG_CTL))
+               if (!(flags & NETFLAG_CTL) && qlength == length)
                {
                        sequence = BigLong(((int *)data)[1]);
                        packetsReceived++;
@@ -778,15 +883,15 @@ int NetConn_ReceivedMessage(netconn_t *conn, qbyte *data, int length)
                        length -= 8;
                        if (flags & NETFLAG_UNRELIABLE)
                        {
-                               if (sequence >= conn->unreliableReceiveSequence)
+                               if (sequence >= conn->nq.unreliableReceiveSequence)
                                {
-                                       if (sequence > conn->unreliableReceiveSequence)
+                                       if (sequence > conn->nq.unreliableReceiveSequence)
                                        {
-                                               count = sequence - conn->unreliableReceiveSequence;
+                                               count = sequence - conn->nq.unreliableReceiveSequence;
                                                droppedDatagrams += count;
                                                Con_DPrintf("Dropped %u datagram(s)\n", count);
                                        }
-                                       conn->unreliableReceiveSequence = sequence + 1;
+                                       conn->nq.unreliableReceiveSequence = sequence + 1;
                                        conn->lastMessageTime = realtime;
                                        conn->timeout = realtime + net_messagetimeout.value;
                                        unreliableMessagesReceived++;
@@ -804,27 +909,53 @@ int NetConn_ReceivedMessage(netconn_t *conn, qbyte *data, int length)
                        }
                        else if (flags & NETFLAG_ACK)
                        {
-                               if (sequence == (conn->sendSequence - 1))
+                               if (sequence == (conn->nq.sendSequence - 1))
                                {
-                                       if (sequence == conn->ackSequence)
+                                       if (sequence == conn->nq.ackSequence)
                                        {
-                                               conn->ackSequence++;
-                                               if (conn->ackSequence != conn->sendSequence)
+                                               conn->nq.ackSequence++;
+                                               if (conn->nq.ackSequence != conn->nq.sendSequence)
                                                        Con_DPrint("ack sequencing error\n");
                                                conn->lastMessageTime = realtime;
                                                conn->timeout = realtime + net_messagetimeout.value;
-                                               conn->sendMessageLength -= MAX_PACKETFRAGMENT;
-                                               if (conn->sendMessageLength > 0)
+                                               if (conn->sendMessageLength > MAX_PACKETFRAGMENT)
                                                {
+                                                       unsigned int packetLen;
+                                                       unsigned int dataLen;
+                                                       unsigned int eom;
+                                                       unsigned int *header;
+
+                                                       conn->sendMessageLength -= MAX_PACKETFRAGMENT;
                                                        memcpy(conn->sendMessage, conn->sendMessage+MAX_PACKETFRAGMENT, conn->sendMessageLength);
-                                                       conn->sendNext = true;
-                                                       NetConn_SendMessageNext(conn);
+
+                                                       if (conn->sendMessageLength <= MAX_PACKETFRAGMENT)
+                                                       {
+                                                               dataLen = conn->sendMessageLength;
+                                                               eom = NETFLAG_EOM;
+                                                       }
+                                                       else
+                                                       {
+                                                               dataLen = MAX_PACKETFRAGMENT;
+                                                               eom = 0;
+                                                       }
+
+                                                       packetLen = NET_HEADERSIZE + dataLen;
+
+                                                       header = (unsigned int *)sendbuffer;
+                                                       header[0] = BigLong(packetLen | (NETFLAG_DATA | eom));
+                                                       header[1] = BigLong(conn->nq.sendSequence);
+                                                       memcpy(sendbuffer + NET_HEADERSIZE, conn->sendMessage, dataLen);
+
+                                                       conn->nq.sendSequence++;
+
+                                                       if (NetConn_Write(conn->mysocket, (void *)&sendbuffer, packetLen, &conn->peeraddress) == (int)packetLen)
+                                                       {
+                                                               conn->lastSendTime = realtime;
+                                                               packetsSent++;
+                                                       }
                                                }
                                                else
-                                               {
                                                        conn->sendMessageLength = 0;
-                                                       conn->canSend = true;
-                                               }
                                        }
                                        else
                                                Con_DPrint("Duplicate ACK received\n");
@@ -838,14 +969,21 @@ int NetConn_ReceivedMessage(netconn_t *conn, qbyte *data, int length)
                                unsigned int temppacket[2];
                                temppacket[0] = BigLong(8 | NETFLAG_ACK);
                                temppacket[1] = BigLong(sequence);
-                               NetConn_Write(conn->mysocket, (qbyte *)temppacket, 8, &conn->peeraddress);
-                               if (sequence == conn->receiveSequence)
+                               NetConn_Write(conn->mysocket, (unsigned char *)temppacket, 8, &conn->peeraddress);
+                               if (sequence == conn->nq.receiveSequence)
                                {
                                        conn->lastMessageTime = realtime;
                                        conn->timeout = realtime + net_messagetimeout.value;
-                                       conn->receiveSequence++;
-                                       memcpy(conn->receiveMessage + conn->receiveMessageLength, data, length);
-                                       conn->receiveMessageLength += length;
+                                       conn->nq.receiveSequence++;
+                                       if( conn->receiveMessageLength + length <= (int)sizeof( conn->receiveMessage ) ) {
+                                               memcpy(conn->receiveMessage + conn->receiveMessageLength, data, length);
+                                               conn->receiveMessageLength += length;
+                                       } else {
+                                               Con_Printf( "Reliable message (seq: %i) too big for message buffer!\n"
+                                                                       "Dropping the message!\n", sequence );
+                                               conn->receiveMessageLength = 0;
+                                               return 1;
+                                       }
                                        if (flags & NETFLAG_EOM)
                                        {
                                                reliableMessagesReceived++;
@@ -869,12 +1007,16 @@ int NetConn_ReceivedMessage(netconn_t *conn, qbyte *data, int length)
        return 0;
 }
 
-void NetConn_ConnectionEstablished(lhnetsocket_t *mysocket, lhnetaddress_t *peeraddress)
+void NetConn_ConnectionEstablished(lhnetsocket_t *mysocket, lhnetaddress_t *peeraddress, protocolversion_t initialprotocol)
 {
        cls.connect_trying = false;
        M_Update_Return_Reason("");
        // the connection request succeeded, stop current connection and set up a new connection
        CL_Disconnect();
+       // if we're connecting to a remote server, shut down any local server
+       if (LHNETADDRESS_GetAddressType(peeraddress) != LHNETADDRESSTYPE_LOOP && sv.active)
+               Host_ShutdownServer ();
+       // allocate a net connection to keep track of things
        cls.netcon = NetConn_Open(mysocket, peeraddress);
        Con_Printf("Connection accepted to %s\n", cls.netcon->address);
        key_dest = key_game;
@@ -882,6 +1024,9 @@ void NetConn_ConnectionEstablished(lhnetsocket_t *mysocket, lhnetaddress_t *peer
        cls.demonum = -1;                       // not in the demo loop now
        cls.state = ca_connected;
        cls.signon = 0;                         // need all the signon messages before playing
+       cls.protocol = initialprotocol;
+       if (cls.protocol == PROTOCOL_QUAKEWORLD)
+               Cmd_ForwardStringToServer("new");
 }
 
 int NetConn_IsLocalGame(void)
@@ -891,14 +1036,17 @@ int NetConn_IsLocalGame(void)
        return false;
 }
 
-int NetConn_ClientParsePacket(lhnetsocket_t *mysocket, qbyte *data, int length, lhnetaddress_t *peeraddress)
+static int NetConn_ClientParsePacket(lhnetsocket_t *mysocket, unsigned char *data, int length, lhnetaddress_t *peeraddress)
 {
+       qboolean fromserver;
        int ret, c, control;
-       lhnetaddress_t svaddress;
        const char *s;
        char *string, addressstring2[128], cname[128], ipstring[32];
        char stringbuf[16384];
 
+       // quakeworld ingame packet
+       fromserver = cls.netcon && mysocket == cls.netcon->mysocket && !LHNETADDRESS_Compare(&cls.netcon->peeraddress, peeraddress);
+
        if (length >= 5 && data[0] == 255 && data[1] == 255 && data[2] == 255 && data[3] == 255)
        {
                // received a command string - strip off the packaging and put it
@@ -910,25 +1058,45 @@ int NetConn_ClientParsePacket(lhnetsocket_t *mysocket, qbyte *data, int length,
                stringbuf[length] = 0;
                string = stringbuf;
 
+               LHNETADDRESS_ToString(peeraddress, addressstring2, sizeof(addressstring2), true);
+
                if (developer.integer)
                {
-                       LHNETADDRESS_ToString(peeraddress, addressstring2, sizeof(addressstring2), true);
                        Con_Printf("NetConn_ClientParsePacket: %s sent us a command:\n", addressstring2);
                        Com_HexDumpToConsole(data, length);
                }
 
                if (length > 10 && !memcmp(string, "challenge ", 10) && cls.connect_trying)
                {
-                       LHNETADDRESS_ToString(peeraddress, addressstring2, sizeof(addressstring2), true);
+                       // darkplaces or quake3
+                       char protocolnames[1400];
+                       Protocol_Names(protocolnames, sizeof(protocolnames));
                        Con_Printf("\"%s\" received, sending connect request back to %s\n", string, addressstring2);
                        M_Update_Return_Reason("Got challenge response");
-                       NetConn_WriteString(mysocket, va("\377\377\377\377connect\\protocol\\darkplaces 3\\challenge\\%s", string + 10), peeraddress);
+                       NetConn_WriteString(mysocket, va("\377\377\377\377connect\\protocol\\darkplaces 3\\protocols\\%s\\challenge\\%s", protocolnames, string + 10), peeraddress);
                        return true;
                }
+               if (length > 1 && string[0] == 'c' && (string[1] == '-' || (string[1] >= '0' && string[1] <= '9')))
+               {
+                       // quakeworld
+                       LHNETADDRESS_ToString(peeraddress, addressstring2, sizeof(addressstring2), true);
+                       Con_Printf("\"%s\" received, sending QuakeWorld connect request back to %s\n", string, addressstring2);
+                       M_Update_Return_Reason("Got QuakeWorld challenge response");
+                       cls.qw_qport = qport.integer;
+                       NetConn_WriteString(mysocket, va("\377\377\377\377connect 28 %i %i \"%s\"\n", cls.qw_qport, atoi(string + 1), cls.userinfo), peeraddress);
+               }
                if (length == 6 && !memcmp(string, "accept", 6) && cls.connect_trying)
                {
+                       // darkplaces or quake3
                        M_Update_Return_Reason("Accepted");
-                       NetConn_ConnectionEstablished(mysocket, peeraddress);
+                       NetConn_ConnectionEstablished(mysocket, peeraddress, PROTOCOL_DARKPLACES3);
+                       return true;
+               }
+               if (length > 1 && string[0] == 'j' && cls.connect_trying)
+               {
+                       // quakeworld
+                       M_Update_Return_Reason("QuakeWorld Accepted");
+                       NetConn_ConnectionEstablished(mysocket, peeraddress, PROTOCOL_QUAKEWORLD);
                        return true;
                }
                if (length > 7 && !memcmp(string, "reject ", 7) && cls.connect_trying)
@@ -945,7 +1113,7 @@ int NetConn_ClientParsePacket(lhnetsocket_t *mysocket, qbyte *data, int length,
                if (length >= 13 && !memcmp(string, "infoResponse\x0A", 13))
                {
                        serverlist_info_t *info;
-                       int i, n;
+                       int n;
                        double pingtime;
 
                        string += 13;
@@ -955,8 +1123,26 @@ int NetConn_ClientParsePacket(lhnetsocket_t *mysocket, qbyte *data, int length,
                        for( n = 0; n < serverlist_cachecount; n++ )
                                if( !strcmp( cname, serverlist_cache[n].info.cname ) )
                                        break;
-                       if( n == serverlist_cachecount )
-                               return true;
+                       if( n == serverlist_cachecount ) {
+                               // LAN search doesnt require an answer from the master server so we wont
+                               // know the ping nor will it be initialized already...
+
+                               // find a slot
+                               if( serverlist_cachecount == SERVERLIST_TOTALSIZE )
+                                       return true;
+
+                               memset(&serverlist_cache[serverlist_cachecount], 0, sizeof(serverlist_cache[serverlist_cachecount]));
+                               // store the data the engine cares about (address and ping)
+                               strlcpy (serverlist_cache[serverlist_cachecount].info.cname, cname, sizeof (serverlist_cache[serverlist_cachecount].info.cname));
+                               serverlist_cache[serverlist_cachecount].info.ping = 100000;
+                               serverlist_cache[serverlist_cachecount].querytime = realtime;
+                               // if not in the slist menu we should print the server to console
+                               if (serverlist_consoleoutput) {
+                                       Con_Printf("querying %s\n", ipstring);
+                               }
+
+                               ++serverlist_cachecount;
+                       }
 
                        info = &serverlist_cache[n].info;
                        if ((s = SearchInfostring(string, "gamename"     )) != NULL) strlcpy(info->game, s, sizeof (info->game));else info->game[0] = 0;
@@ -978,31 +1164,17 @@ int NetConn_ClientParsePacket(lhnetsocket_t *mysocket, qbyte *data, int length,
                        // legacy/old stuff move it to the menu ASAP
 
                        // build description strings for the things users care about
-                       dpsnprintf(serverlist_cache[n].line1, sizeof(serverlist_cache[n].line1), "%5d%c%3u/%3u %-65.65s", (int)pingtime, info->protocol != NET_PROTOCOL_VERSION ? '*' : ' ', info->numplayers, info->maxplayers, info->name);
+                       dpsnprintf(serverlist_cache[n].line1, sizeof(serverlist_cache[n].line1), "%c%c%5d%c%c%c%3u/%3u %-65.65s", STRING_COLOR_TAG, pingtime >= 300 ? '1' : (pingtime >= 200 ? '3' : '7'), (int)pingtime, STRING_COLOR_TAG, STRING_COLOR_DEFAULT + '0', info->protocol != NET_PROTOCOL_VERSION ? '*' : ' ', info->numplayers, info->maxplayers, info->name);
                        dpsnprintf(serverlist_cache[n].line2, sizeof(serverlist_cache[n].line2), "%-21.21s %-19.19s %-17.17s %-20.20s", info->cname, info->game, info->mod, info->map);
-                       // if ping is especially high, display it as such
-                       if (pingtime >= 300)
-                       {
-                               // orange numbers (lower block)
-                               for (i = 0;i < 5;i++)
-                                       if (serverlist_cache[n].line1[i] != ' ')
-                                               serverlist_cache[n].line1[i] += 128;
-                       }
-                       else if (pingtime >= 200)
-                       {
-                               // yellow numbers (in upper block)
-                               for (i = 0;i < 5;i++)
-                                       if (serverlist_cache[n].line1[i] != ' ')
-                                               serverlist_cache[n].line1[i] -= 30;
+                       if( serverlist_cache[n].query == SQS_QUERIED ) {
+                               ServerList_ViewList_Remove( &serverlist_cache[n] );
                        }
-                       // and finally, update the view set
-                       if( serverlist_cache[n].finished )
-                ServerList_ViewList_Remove( &serverlist_cache[n] );
-                       // else if not in the slist menu we should print the server to console (if wanted)
+                       // if not in the slist menu we should print the server to console (if wanted)
                        else if( serverlist_consoleoutput )
                                Con_Printf("%s\n%s\n", serverlist_cache[n].line1, serverlist_cache[n].line2);
+                       // and finally, update the view set
                        ServerList_ViewList_Insert( &serverlist_cache[n] );
-                       serverlist_cache[n].finished = true;
+                       serverlist_cache[n].query = SQS_QUERIED;
 
                        return true;
                }
@@ -1032,17 +1204,11 @@ int NetConn_ClientParsePacket(lhnetsocket_t *mysocket, qbyte *data, int length,
                                {
                                        serverquerycount++;
 
-                                       LHNETADDRESS_FromString(&svaddress, ipstring, 0);
-                                       NetConn_WriteString(mysocket, "\377\377\377\377getinfo", &svaddress);
-
                                        memset(&serverlist_cache[serverlist_cachecount], 0, sizeof(serverlist_cache[serverlist_cachecount]));
                                        // store the data the engine cares about (address and ping)
                                        strlcpy (serverlist_cache[serverlist_cachecount].info.cname, ipstring, sizeof (serverlist_cache[serverlist_cachecount].info.cname));
                                        serverlist_cache[serverlist_cachecount].info.ping = 100000;
-                                       serverlist_cache[serverlist_cachecount].querytime = realtime;
-                                       // if not in the slist menu we should print the server to console
-                                       if (serverlist_consoleoutput)
-                                               Con_Printf("querying %s\n", ipstring);
+                                       serverlist_cache[serverlist_cachecount].query = SQS_QUERYING;
 
                                        ++serverlist_cachecount;
                                }
@@ -1051,6 +1217,8 @@ int NetConn_ClientParsePacket(lhnetsocket_t *mysocket, qbyte *data, int length,
                                data += 7;
                                length -= 7;
                        }
+                       // begin or resume serverlist queries
+                       serverlist_querysleep = false;
                        return true;
                }
                /*
@@ -1064,10 +1232,47 @@ int NetConn_ClientParsePacket(lhnetsocket_t *mysocket, qbyte *data, int length,
                if (!strncmp(string, "ack", 3))
                        return true;
                */
+               // QuakeWorld compatibility
+               if (length >= 1 && string[0] == 'j' && cls.connect_trying)
+               {
+                       // accept message
+                       M_Update_Return_Reason("Accepted");
+                       NetConn_ConnectionEstablished(mysocket, peeraddress, PROTOCOL_QUAKEWORLD);
+                       return true;
+               }
+               if (length > 1 && string[0] == 'c' && string[1] >= '0' && string[1] <= '9' && cls.connect_trying)
+               {
+                       // challenge message
+                       LHNETADDRESS_ToString(peeraddress, addressstring2, sizeof(addressstring2), true);
+                       Con_Printf("challenge %s received, sending connect request back to %s\n", string + 1, addressstring2);
+                       M_Update_Return_Reason("Got challenge response");
+                       cls.qw_qport = qport.integer;
+                       InfoString_SetValue(cls.userinfo, sizeof(cls.userinfo), "*ip", addressstring2);
+                       InfoString_SetValue(cls.userinfo, sizeof(cls.userinfo), "name", cl_name.string);
+                       InfoString_SetValue(cls.userinfo, sizeof(cls.userinfo), "topcolor", va("%i", (cl_color.integer >> 4) & 15));
+                       InfoString_SetValue(cls.userinfo, sizeof(cls.userinfo), "bottomcolor", va("%i", (cl_color.integer) & 15));
+                       InfoString_SetValue(cls.userinfo, sizeof(cls.userinfo), "rate", va("%i", cl_rate.integer));
+                       InfoString_SetValue(cls.userinfo, sizeof(cls.userinfo), "msg", "1");
+                       InfoString_SetValue(cls.userinfo, sizeof(cls.userinfo), "*ver", engineversion);
+                       NetConn_WriteString(mysocket, va("\377\377\377\377connect %i %i %i \"%s\"\n", 28, cls.qw_qport, atoi(string + 1), cls.userinfo), peeraddress);
+                       return true;
+               }
+               if (string[0] == 'n')
+               {
+                       // qw print command
+                       Con_Printf("QW print command from server at %s:\n", addressstring2, string + 1);
+               }
                // we may not have liked the packet, but it was a command packet, so
                // we're done processing this packet now
                return true;
        }
+       // quakeworld ingame packet
+       if (fromserver && cls.protocol == PROTOCOL_QUAKEWORLD && length >= 8 && (ret = NetConn_ReceivedMessage(cls.netcon, data, length, cls.protocol)) == 2)
+       {
+               ret = 0;
+               CL_ParseServerMessage();
+               return ret;
+       }
        // netquake control packets, supported for compatibility only
        if (length >= 5 && (control = BigLong(*((int *)data))) && (control & (~NETFLAG_LENGTH_MASK)) == (int)NETFLAG_CTL && (control & NETFLAG_LENGTH_MASK) == length)
        {
@@ -1092,14 +1297,14 @@ int NetConn_ClientParsePacket(lhnetsocket_t *mysocket, qbyte *data, int length,
                                        LHNETADDRESS_SetPort(&clientportaddress, port);
                                }
                                M_Update_Return_Reason("Accepted");
-                               NetConn_ConnectionEstablished(mysocket, &clientportaddress);
+                               NetConn_ConnectionEstablished(mysocket, &clientportaddress, PROTOCOL_QUAKE);
                        }
                        break;
                case CCREP_REJECT:
                        if (developer.integer)
                                Con_Printf("Datagram_ParseConnectionless: received CCREP_REJECT from %s.\n", addressstring2);
                        cls.connect_trying = false;
-                       M_Update_Return_Reason(data);
+                       M_Update_Return_Reason((char *)data);
                        break;
 #if 0
                case CCREP_SERVER_INFO:
@@ -1155,17 +1360,81 @@ int NetConn_ClientParsePacket(lhnetsocket_t *mysocket, qbyte *data, int length,
                return true;
        }
        ret = 0;
-       if (length >= (int)NET_HEADERSIZE && cls.netcon && mysocket == cls.netcon->mysocket && !LHNETADDRESS_Compare(&cls.netcon->peeraddress, peeraddress) && (ret = NetConn_ReceivedMessage(cls.netcon, data, length)) == 2)
+       if (fromserver && length >= (int)NET_HEADERSIZE && (ret = NetConn_ReceivedMessage(cls.netcon, data, length, cls.protocol)) == 2)
                CL_ParseServerMessage();
        return ret;
 }
 
+void NetConn_QueryQueueFrame(void)
+{
+       int index;
+       int queries;
+       int maxqueries;
+       double timeouttime;
+       static double querycounter = 0;
+
+       if (serverlist_querysleep)
+               return;
+
+       // each time querycounter reaches 1.0 issue a query
+       querycounter += host_realframetime * net_slist_queriespersecond.value;
+       maxqueries = (int)querycounter;
+       maxqueries = bound(0, maxqueries, net_slist_queriesperframe.integer);
+       querycounter -= maxqueries;
+
+       if( maxqueries == 0 ) {
+               return;
+       }
+
+       // scan serverlist and issue queries as needed
+    serverlist_querysleep = true;
+
+       timeouttime = realtime - net_slist_timeout.value;
+       for( index = 0, queries = 0 ; index < serverlist_cachecount && queries < maxqueries ; index++ )
+       {
+               serverlist_entry_t *entry = &serverlist_cache[ index ];
+               if( entry->query != SQS_QUERYING )
+               {
+                       continue;
+               }
+
+        serverlist_querysleep = false;
+               if( entry->querycounter != 0 && entry->querytime > timeouttime )
+               {
+                       continue;
+               }
+
+               if( entry->querycounter != (unsigned) net_slist_maxtries.integer )
+               {
+                       lhnetaddress_t address;
+                       int socket;
+
+                       LHNETADDRESS_FromString(&address, entry->info.cname, 0);
+                       for (socket = 0; socket < cl_numsockets ; socket++) {
+                               NetConn_WriteString(cl_sockets[socket], "\377\377\377\377getinfo", &address);
+                       }
+
+                       entry->querytime = realtime;
+                       entry->querycounter++;
+
+                       // if not in the slist menu we should print the server to console
+                       if (serverlist_consoleoutput)
+                               Con_Printf("querying %25s (%i. try)\n", entry->info.cname, entry->querycounter);
+
+                       queries++;
+               }
+               else
+               {
+                       entry->query = SQS_TIMEDOUT;
+               }
+       }
+}
+
 void NetConn_ClientFrame(void)
 {
        int i, length;
        lhnetaddress_t peeraddress;
-       netconn_t *conn;
-       NetConn_UpdateServerStuff();
+       NetConn_UpdateSockets();
        if (cls.connect_trying && cls.connect_nextsendtime < realtime)
        {
                if (cls.connect_remainingtries == 0)
@@ -1178,7 +1447,7 @@ void NetConn_ClientFrame(void)
                        M_Update_Return_Reason("Connect: Failed");
                        return;
                }
-               // try challenge first (newer server)
+               // try challenge first (newer DP server or QW)
                NetConn_WriteString(cls.connect_mysocket, "\377\377\377\377getchallenge", &cls.connect_address);
                // then try netquake as a fallback (old server, or netquake)
                SZ_Clear(&net_message);
@@ -1194,18 +1463,17 @@ void NetConn_ClientFrame(void)
        for (i = 0;i < cl_numsockets;i++)
                while (cl_sockets[i] && (length = NetConn_Read(cl_sockets[i], readbuffer, sizeof(readbuffer), &peeraddress)) > 0)
                        NetConn_ClientParsePacket(cl_sockets[i], readbuffer, length, &peeraddress);
+       NetConn_QueryQueueFrame();
        if (cls.netcon && realtime > cls.netcon->timeout)
        {
                Con_Print("Connection timed out\n");
                CL_Disconnect();
-               Host_ShutdownServer (false);
+               Host_ShutdownServer ();
        }
-       for (conn = netconn_list;conn;conn = conn->next)
-               NetConn_ReSendMessage(conn);
 }
 
 #define MAX_CHALLENGES 128
-struct
+struct challenge_s
 {
        lhnetaddress_t address;
        double time;
@@ -1228,13 +1496,96 @@ static void NetConn_BuildChallengeString(char *buffer, int bufferlength)
        buffer[i] = 0;
 }
 
-int NetConn_ServerParsePacket(lhnetsocket_t *mysocket, qbyte *data, int length, lhnetaddress_t *peeraddress)
+static qboolean NetConn_BuildStatusResponse(const char* challenge, char* out_msg, size_t out_size, qboolean fullstatus)
 {
-       int i, n, ret, clientnum, responselength, best;
+       unsigned int nb_clients = 0, i;
+       int length;
+
+       // How many clients are there?
+       for (i = 0;i < (unsigned int)svs.maxclients;i++)
+               if (svs.clients[i].active)
+                       nb_clients++;
+
+       // TODO: we should add more information for the full status string
+       length = dpsnprintf(out_msg, out_size,
+                                               "\377\377\377\377%s\x0A"
+                                               "\\gamename\\%s\\modname\\%s\\sv_maxclients\\%d"
+                                               "\\clients\\%d\\mapname\\%s\\hostname\\%s""\\protocol\\%d"
+                                               "%s%s"
+                                               "%s",
+                                               fullstatus ? "statusResponse" : "infoResponse",
+                                               gamename, com_modname, svs.maxclients,
+                                               nb_clients, sv.name, hostname.string, NET_PROTOCOL_VERSION,
+                                               challenge ? "\\challenge\\" : "", challenge ? challenge : "",
+                                               fullstatus ? "\n" : "");
+
+       // Make sure it fits in the buffer
+       if (length < 0)
+               return false;
+
+       if (fullstatus)
+       {
+               char *ptr;
+               int left;
+
+               ptr = out_msg + length;
+               left = (int)out_size - length;
+
+               for (i = 0;i < (unsigned int)svs.maxclients;i++)
+               {
+                       client_t *cl = &svs.clients[i];
+                       if (cl->active)
+                       {
+                               int nameind, cleanind;
+                               char curchar;
+                               char cleanname [sizeof(cl->name)];
+
+                               // Remove all characters '"' and '\' in the player name
+                               nameind = 0;
+                               cleanind = 0;
+                               do
+                               {
+                                       curchar = cl->name[nameind++];
+                                       if (curchar != '"' && curchar != '\\')
+                                       {
+                                               cleanname[cleanind++] = curchar;
+                                               if (cleanind == sizeof(cleanname) - 1)
+                                                       break;
+                                       }
+                               } while (curchar != '\0');
+
+                               length = dpsnprintf(ptr, left, "%d %d \"%s\"\n",
+                                                                       cl->frags,
+                                                                       (int)(cl->ping * 1000.0f),
+                                                                       cleanname);
+                               if(length < 0)
+                                       return false;
+                               left -= length;
+                               ptr += length;
+                       }
+               }
+       }
+
+       return true;
+}
+
+extern void SV_SendServerinfo (client_t *client);
+static int NetConn_ServerParsePacket(lhnetsocket_t *mysocket, unsigned char *data, int length, lhnetaddress_t *peeraddress)
+{
+       int i, ret, clientnum, best;
        double besttime;
        client_t *client;
        netconn_t *conn;
-       char *s, *string, response[512], addressstring2[128], stringbuf[16384];
+       char *s, *string, response[1400], addressstring2[128], stringbuf[16384];
+
+       // see if we can identify the sender as a local player
+       // (this is necessary for rcon to send a reliable reply if the client is
+       //  actually on the server, not sending remotely)
+       for (i = 0, host_client = svs.clients;i < svs.maxclients;i++, host_client++)
+               if (host_client->netconnection && host_client->netconnection->mysocket == mysocket && !LHNETADDRESS_Compare(&host_client->netconnection->peeraddress, peeraddress))
+                       break;
+       if (i == svs.maxclients)
+               host_client = NULL;
 
        if (sv.active)
        {
@@ -1303,24 +1654,27 @@ int NetConn_ServerParsePacket(lhnetsocket_t *mysocket, qbyte *data, int length,
                                                        for (clientnum = 0, client = svs.clients;clientnum < svs.maxclients;clientnum++, client++)
                                                                if (client->netconnection && LHNETADDRESS_Compare(peeraddress, &client->netconnection->peeraddress) == 0)
                                                                        break;
-                                                       if (clientnum < svs.maxclients)
+                                                       if (clientnum < svs.maxclients && realtime - client->connecttime < net_messagerejointimeout.value)
+                                                       {
+                                                               // client is still trying to connect,
+                                                               // so we send a duplicate reply
+                                                               if (developer.integer)
+                                                                       Con_Printf("Datagram_ParseConnectionless: sending duplicate accept to %s.\n", addressstring2);
+                                                               NetConn_WriteString(mysocket, "\377\377\377\377accept", peeraddress);
+                                                       }
+#if 0
+                                                       else if (clientnum < svs.maxclients)
                                                        {
-                                                               // duplicate connection request
-                                                               if (realtime - client->connecttime < 2.0)
-                                                               {
-                                                                       // client is still trying to connect,
-                                                                       // so we send a duplicate reply
-                                                                       if (developer.integer)
-                                                                               Con_Printf("Datagram_ParseConnectionless: sending duplicate accept to %s.\n", addressstring2);
-                                                                       NetConn_WriteString(mysocket, "\377\377\377\377accept", peeraddress);
-                                                               }
-                                                               // only kick if old connection seems dead
                                                                if (realtime - client->netconnection->lastMessageTime >= net_messagerejointimeout.value)
                                                                {
-                                                                       // kick off connection and await retry
-                                                                       client->deadsocket = true;
+                                                                       // client crashed and is coming back, keep their stuff intact
+                                                                       SV_SendServerinfo(client);
+                                                                       //host_client = client;
+                                                                       //SV_DropClient (true);
                                                                }
+                                                               // else ignore them
                                                        }
+#endif
                                                        else
                                                        {
                                                                // this is a new client, find a slot
@@ -1338,7 +1692,9 @@ int NetConn_ServerParsePacket(lhnetsocket_t *mysocket, qbyte *data, int length,
                                                                                        Con_Printf("Datagram_ParseConnectionless: sending \"accept\" to %s.\n", conn->address);
                                                                                NetConn_WriteString(mysocket, "\377\377\377\377accept", peeraddress);
                                                                                // now set up the client
+                                                                               SV_VM_Begin();
                                                                                SV_ConnectClient(clientnum, conn);
+                                                                               SV_VM_End();
                                                                                NetConn_Heartbeat(1);
                                                                        }
                                                                }
@@ -1358,19 +1714,12 @@ int NetConn_ServerParsePacket(lhnetsocket_t *mysocket, qbyte *data, int length,
                        if (length >= 7 && !memcmp(string, "getinfo", 7))
                        {
                                const char *challenge = NULL;
+
                                // If there was a challenge in the getinfo message
                                if (length > 8 && string[7] == ' ')
                                        challenge = string + 8;
-                               for (i = 0, n = 0;i < svs.maxclients;i++)
-                                       if (svs.clients[i].active)
-                                               n++;
-                               responselength = dpsnprintf(response, sizeof(response), "\377\377\377\377infoResponse\x0A"
-                                                       "\\gamename\\%s\\modname\\%s\\sv_maxclients\\%d"
-                                                       "\\clients\\%d\\mapname\\%s\\hostname\\%s\\protocol\\%d%s%s",
-                                                       gamename, com_modname, svs.maxclients, n,
-                                                       sv.name, hostname.string, NET_PROTOCOL_VERSION, challenge ? "\\challenge\\" : "", challenge ? challenge : "");
-                               // does it fit in the buffer?
-                               if (responselength >= 0)
+
+                               if (NetConn_BuildStatusResponse(challenge, response, sizeof(response), false))
                                {
                                        if (developer.integer)
                                                Con_Printf("Sending reply to master %s - %s\n", addressstring2, response);
@@ -1378,6 +1727,62 @@ int NetConn_ServerParsePacket(lhnetsocket_t *mysocket, qbyte *data, int length,
                                }
                                return true;
                        }
+                       if (length >= 9 && !memcmp(string, "getstatus", 9))
+                       {
+                               const char *challenge = NULL;
+
+                               // If there was a challenge in the getinfo message
+                               if (length > 10 && string[9] == ' ')
+                                       challenge = string + 10;
+
+                               if (NetConn_BuildStatusResponse(challenge, response, sizeof(response), true))
+                               {
+                                       if (developer.integer)
+                                               Con_Printf("Sending reply to client %s - %s\n", addressstring2, response);
+                                       NetConn_WriteString(mysocket, response, peeraddress);
+                               }
+                               return true;
+                       }
+                       if (length >= 5 && !memcmp(string, "rcon ", 5))
+                       {
+                               int i;
+                               char *s = string + 5;
+                               char password[64];
+                               for (i = 0;*s > ' ';s++)
+                                       if (i < (int)sizeof(password) - 1)
+                                               password[i++] = *s;
+                               password[i] = 0;
+                               if (password[0] > ' ' && !strcmp(rcon_password.string, password))
+                               {
+                                       // looks like a legitimate rcon command with the correct password
+                                       Con_Printf("server received rcon command from %s:\n%s\n", host_client ? host_client->name : addressstring2, s);
+                                       rcon_redirect = true;
+                                       rcon_redirect_bufferpos = 0;
+                                       Cmd_ExecuteString(s, src_command);
+                                       rcon_redirect_buffer[rcon_redirect_bufferpos] = 0;
+                                       rcon_redirect = false;
+                                       // print resulting text to client
+                                       // if client is playing, send a reliable reply instead of
+                                       // a command packet
+                                       if (host_client)
+                                       {
+                                               // if the netconnection is loop, then this is the
+                                               // local player on a listen mode server, and it would
+                                               // result in duplicate printing to the console
+                                               // (not that the local player should be using rcon
+                                               //  when they have the console)
+                                               if (host_client->netconnection && LHNETADDRESS_GetAddressType(&host_client->netconnection->peeraddress) != LHNETADDRESSTYPE_LOOP)
+                                                       SV_ClientPrintf("%s", rcon_redirect_buffer);
+                                       }
+                                       else
+                                       {
+                                               // qw print command
+                                               dpsnprintf(response, sizeof(response), "\377\377\377\377n%s", rcon_redirect_buffer);
+                                               NetConn_WriteString(mysocket, response, peeraddress);
+                                       }
+                               }
+                               return true;
+                       }
                        /*
                        if (!strncmp(string, "ping", 4))
                        {
@@ -1448,13 +1853,17 @@ int NetConn_ServerParsePacket(lhnetsocket_t *mysocket, qbyte *data, int length,
                                                                        NetConn_Write(mysocket, net_message.data, net_message.cursize, peeraddress);
                                                                        SZ_Clear(&net_message);
                                                                }
+#if 0
                                                                else if (realtime - client->netconnection->lastMessageTime >= net_messagerejointimeout.value)
                                                                {
+                                                                       SV_SendServerinfo(client);
                                                                        // the old client hasn't sent us anything
                                                                        // in quite a while, so kick off and let
                                                                        // the retry take care of it...
-                                                                       client->deadsocket = true;
+                                                                       //host_client = client;
+                                                                       //SV_DropClient (true);
                                                                }
+#endif
                                                        }
                                                        else
                                                        {
@@ -1479,7 +1888,9 @@ int NetConn_ServerParsePacket(lhnetsocket_t *mysocket, qbyte *data, int length,
                                                                        NetConn_Write(mysocket, net_message.data, net_message.cursize, peeraddress);
                                                                        SZ_Clear(&net_message);
                                                                        // now set up the client struct
+                                                                       SV_VM_Begin();
                                                                        SV_ConnectClient(clientnum, conn);
+                                                                       SV_VM_End();
                                                                        NetConn_Heartbeat(1);
                                                                }
                                                                else
@@ -1546,7 +1957,7 @@ int NetConn_ServerParsePacket(lhnetsocket_t *mysocket, qbyte *data, int length,
                                                        MSG_WriteByte(&net_message, playerNumber);
                                                        MSG_WriteString(&net_message, client->name);
                                                        MSG_WriteLong(&net_message, client->colors);
-                                                       MSG_WriteLong(&net_message, (int)client->edict->v->frags);
+                                                       MSG_WriteLong(&net_message, (int)client->edict->fields.server->frags);
                                                        MSG_WriteLong(&net_message, (int)(realtime - client->connecttime));
                                                        MSG_WriteString(&net_message, client->netconnection ? client->netconnection->address : "botclient");
                                                        *((int *)net_message.data) = BigLong(NETFLAG_CTL | (net_message.cursize & NETFLAG_LENGTH_MASK));
@@ -1592,12 +2003,13 @@ int NetConn_ServerParsePacket(lhnetsocket_t *mysocket, qbyte *data, int length,
                        }
                }
 #endif
-               for (i = 0, host_client = svs.clients;i < svs.maxclients;i++, host_client++)
+               if (host_client)
                {
-                       if (host_client->netconnection && host_client->netconnection->mysocket == mysocket && !LHNETADDRESS_Compare(&host_client->netconnection->peeraddress, peeraddress))
+                       if ((ret = NetConn_ReceivedMessage(host_client->netconnection, data, length, sv.protocol)) == 2)
                        {
-                               if ((ret = NetConn_ReceivedMessage(host_client->netconnection, data, length)) == 2)
-                                       SV_ReadClientMessage();
+                               SV_VM_Begin();
+                               SV_ReadClientMessage();
+                               SV_VM_End();
                                return ret;
                        }
                }
@@ -1609,8 +2021,7 @@ void NetConn_ServerFrame(void)
 {
        int i, length;
        lhnetaddress_t peeraddress;
-       netconn_t *conn;
-       NetConn_UpdateServerStuff();
+       NetConn_UpdateSockets();
        for (i = 0;i < sv_numsockets;i++)
                while (sv_sockets[i] && (length = NetConn_Read(sv_sockets[i], readbuffer, sizeof(readbuffer), &peeraddress)) > 0)
                        NetConn_ServerParsePacket(sv_sockets[i], readbuffer, length, &peeraddress);
@@ -1623,8 +2034,6 @@ void NetConn_ServerFrame(void)
                        SV_DropClient(false);
                }
        }
-       for (conn = netconn_list;conn;conn = conn->next)
-               NetConn_ReSendMessage(conn);
 }
 
 void NetConn_QueryMasters(void)
@@ -1713,43 +2122,6 @@ void NetConn_Heartbeat(int priority)
        }
 }
 
-int NetConn_SendToAll(sizebuf_t *data, double blocktime)
-{
-       int i, count = 0;
-       qbyte sent[MAX_SCOREBOARD];
-
-       memset(sent, 0, sizeof(sent));
-
-       // simultaneously wait for the first CanSendMessage and send the message,
-       // then wait for a second CanSendMessage (verifying it was received), or
-       // the client drops and is no longer counted
-       // the loop aborts when either it runs out of clients to send to, or a
-       // timeout expires
-       blocktime += Sys_DoubleTime();
-       do
-       {
-               count = 0;
-               NetConn_ClientFrame();
-               NetConn_ServerFrame();
-               for (i = 0, host_client = svs.clients;i < svs.maxclients;i++, host_client++)
-               {
-                       if (host_client->netconnection)
-                       {
-                               if (NetConn_CanSendMessage(host_client->netconnection))
-                               {
-                                       if (!sent[i])
-                                               NetConn_SendReliableMessage(host_client->netconnection, data);
-                                       sent[i] = true;
-                               }
-                               if (!NetConn_CanSendMessage(host_client->netconnection))
-                                       count++;
-                       }
-               }
-       }
-       while (count && Sys_DoubleTime() < blocktime);
-       return count;
-}
-
 static void Net_Heartbeat_f(void)
 {
        if (sv.active)
@@ -1760,7 +2132,10 @@ static void Net_Heartbeat_f(void)
 
 void PrintStats(netconn_t *conn)
 {
-       Con_Printf("address=%21s canSend=%u sendSeq=%6u recvSeq=%6u\n", conn->address, conn->canSend, conn->sendSequence, conn->receiveSequence);
+       if ((cls.state == ca_connected && cls.protocol == PROTOCOL_QUAKEWORLD) || (sv.active && sv.protocol == PROTOCOL_QUAKEWORLD))
+               Con_Printf("address=%21s canSend=%u sendSeq=%6u recvSeq=%6u\n", conn->address, !conn->sendMessageLength, conn->qw.outgoing_sequence, conn->qw.incoming_sequence);
+       else
+               Con_Printf("address=%21s canSend=%u sendSeq=%6u recvSeq=%6u\n", conn->address, !conn->sendMessageLength, conn->nq.sendSequence, conn->nq.receiveSequence);
 }
 
 void Net_Stats_f(void)
@@ -1799,9 +2174,13 @@ void NetConn_Init(void)
        int i;
        lhnetaddress_t tempaddress;
        netconn_mempool = Mem_AllocPool("network connections", 0, NULL);
-       Cmd_AddCommand("net_stats", Net_Stats_f);
-       Cmd_AddCommand("net_slist", Net_Slist_f);
-       Cmd_AddCommand("heartbeat", Net_Heartbeat_f);
+       Cmd_AddCommand("net_stats", Net_Stats_f, "print network statistics");
+       Cmd_AddCommand("net_slist", Net_Slist_f, "query master series and print all server information");
+       Cmd_AddCommand("heartbeat", Net_Heartbeat_f, "send a heartbeat to the master server (updates your server information)");
+       Cvar_RegisterVariable(&net_slist_queriespersecond);
+       Cvar_RegisterVariable(&net_slist_queriesperframe);
+       Cvar_RegisterVariable(&net_slist_timeout);
+       Cvar_RegisterVariable(&net_slist_maxtries);
        Cvar_RegisterVariable(&net_messagetimeout);
        Cvar_RegisterVariable(&net_messagerejointimeout);
        Cvar_RegisterVariable(&net_connecttimeout);