if(global.pnum > 0)
{

loopcount = 0;
while(global.pnum > 0 && loopcount < 250)
{

var pid, size, upid;
//Check for messages from all players (Server only)
for(pid = -1; pid < global.pindex; pid += 1)
{
  size = 0;
  upid = -1; //Used for UDP receives
  clearbuffer();
  if(pid == -1) //Check UDP socket for messages
  {
    size = receivemessage(global.udpsocket);
    if(size > 0)
    {
      upid = ip2pid(lastinIP());
      dynamicaddline("UDP message received. IP-based PID: " + string(upid) + " Msg Size: " + string(size));
      break;
    }
  }
  else if(global.p_online[pid])
  {
    upid = pid;
    size = receivemessage(global.p_tcpsocket[pid]);
    if(size >= 0) break;
  }
}

if(size < 0) break;
if(size == 0 && global.p_online[upid])
{
  debug_log("Uninit PID " + string(upid));
  caddline(global.p_name[upid] + " has logged out.");
  allupdate[5] = upid+1;
  uninit_user(upid);
  if(global.pindex == 0) break;
}

//A loop to get any messages from the current player
//Message ID's:
//-------------
//  0 = [TCP] Player's username (Only reported upon login.)
//  1 = [TCP] Room change
//  2 = [UDP] Player's sprite index
//  3 = [UDP] Player's x,y pos
//  4 = [UDP] A new line of chat
//  5 | [TCP] User log on/off (Only received by clients)
  if(size != 0) messageid = readbyte();
  else messageid = -1;
  //caddline("Got byte of ID " + string(messageid) + " from pid " + string(pid));
  
  switch(messageid)
  {
    case 0:
      /*global.p_name[pid]*/tempstr = readstring(); //p_name is set in case 5.
      tempstr = global.p_name[pid] + " (PID \#" + string(pid) + ") has connected to Battle Network Online.";
      if(global.p_tcprec[pid]) tempstr += " (TCP-Rec)";
      dynamicaddline(tempstr);
      allupdate[0] = 1;
    break;
    
    case 1:
      updateonly = readbyte();
      tempint = readint();
      if(!updateonly)
      {
      global.p_room[pid] = tempint;
      if(tempint == 49) global.dabypass[global.p_uid[pid]] = 1;
      else global.dabypass[global.p_uid[pid]] = 0;
      dynamicaddline(global.p_name[pid] + " has changed to room " + string(tempint) + ".");
      }
      //Send the user a list of data from players in the room they entered:
      if(tempint != Online_Command_Screen)
      {
      clearbuffer();
      writebyte(11);
      rplayers = 0;
      for(ru = 0; ru < global.pindex; ru += 1)
      {
        rpid[rplayers] = 0;
        if(global.p_online[ru] && global.p_room[ru] == tempint && ru != pid)
        {
          rpid[rplayers] = ru;
          rplayers += 1;
        }
      }
      writebyte(rplayers);
      for(ru = 0; ru < rplayers; ru += 1)
      {
        writebyte(rpid[ru]);
        writeint(global.p_x[rpid[ru]]);
        writeint(global.p_y[rpid[ru]]);
        writeint(global.p_spr[rpid[ru],0]);
        for(tempint = 1; tempint <= 5; tempint += 1)
        {
        writestring(global.p_spr[rpid[ru],tempint]);
        }
        writebyte(global.p_canduo[rpid[ru]]);
        writebyte(global.p_induo[rpid[ru]]);
        writebyte(global.p_watchable[rpid[ru]]);
      }
      sendmessage(global.p_tcpsocket[pid]);
      }
      //End room update.
      //dynamicaddline("Room update sent to " + global.p_name[pid] + ".");
      if(!updateonly) allupdate[1] = 1;
      
      //Update their new message board messages.
      clearbuffer();
      writebyte(19);
      for(op = 0; op < global.mb_total[0]; op += 1)
      {
      writebyte(global.u_mb_newmsg[global.p_uid[pid],op]);
      }
      sendmessage(global.p_tcpsocket[pid]);
    break;

    case 2:
      if(upid < 0)
      {
        upid = pid;
        caddline("UPID was less than 0.");
      }
      global.p_spr[upid,0] = readint();
      for(tempint = 1; tempint <= 5; tempint += 1)
      {
      global.p_spr[upid,tempint] = readstring();
      }
      allupdate[2] = 1;
    break;

    case 3:
      if(upid < 0)
      {
        upid = pid;
        caddline("UPID was less than 0.");
      }
      global.p_x[upid] = readint();
      global.p_y[upid] = readint();
      global.p_fps[upid] = readbyte();
      global.p_xdif[upid] = readfloat();
      global.p_ydif[upid] = readfloat();
      caddline("Movement message. X: " + string(global.p_x[upid]));
      allupdate[3] = 1;
    break;

    case 4:
      bytetemp = readbyte();
      if(bytetemp == 99)
      {
      strtemp = readstring();
      dynamicaddline(global.p_name[pid] + ":>> " + strtemp + "[Room " + string(global.p_room[pid]) + "]");
      allupdate[4] = 1;
      }
      else if(global.p_online[bytetemp])
      {
      strtemp = readstring();
      dynamicaddline(global.p_name[pid] + " > " + global.p_name[bytetemp] + ":)) " + strtemp + "[Room " + string(global.p_room[pid]) + "]");
      clearbuffer();
      writebyte(4);
      writebyte(pid);
      writebyte(1);
      writestring(strtemp);
      sendmessage(global.p_tcpsocket[bytetemp]);
      }
    break;
    
    case 5:
      global.p_locip[pid] = readstring();
      tempuname = readstring();
      temppwd = readstring();
      global.p_tcprec[pid] = readbyte();
      copy = 0;
      suclogin = -1;
      //Alt login check:
      for(ipc = 0; ipc < global.pindex; ipc += 1)
      {
      if(global.p_online[ipc] && ipc != pid)
      {
      if(global.p_ip[ipc] == global.p_ip[pid] && global.p_locip[ipc] == global.p_locip[pid] && global.p_name[ipc] != "Saber Mage" && global.p_name[ipc] != "Vance Serori" /*&& mplay_ipaddress() != global.p_locip[pid]*/)
      {
      copy = 1;
      dynamicaddline("User initialization halted - alt login attempt by " + global.p_name[ipc] + "(PID \#" + string(ipc) + ").");
      }
      }
      if(copy) break;
      }
      //Username/Password Check
      for(ipc = 0; ipc < global.u_total; ipc += 1)
      {
      if(tempuname == global.u_name[ipc] && temppwd == global.u_pwd[ipc]) suclogin = ipc;
      if(suclogin >= 0) break;
      }
      clearbuffer();
      writebyte(8);
      if(!copy && suclogin >= 0 && !global.maintmode)
      {
      global.p_uid[pid] = suclogin;
      global.p_name[pid] = global.u_name[suclogin];
      global.u_pid[suclogin] = pid;
      writeint(global.playerudpport);
      writebyte(global.dabypass[global.p_uid[pid]]);
      writeint(global.latestclient);
      writestring(global.servermsg);
      //Write their inventory for them to update to:
      writebyte(global.iv_totalcats);
      for(ivc = 0; ivc < global.iv_totalcats; ivc += 1)
      {
      writeint(uinv_get(global.p_uid[pid],ivc,0));
      for(ivi = 1; ivi <= uinv_get(global.p_uid[pid],ivc,0); ivi += 1)
        writestring(uinv_get(global.p_uid[pid],ivc,ivi));
      }
      //End inventory write.
      //Write their activated hexbridges:
      writeint(global.hxbridge[global.p_uid[pid],0]);
      for(ivc = 1; ivc <= global.hxbridge[global.p_uid[pid],0]; ivc += 1)
        writestring(global.hxbridge[global.p_uid[pid],ivc]);
      //End hexbridge write.
      }
      else
      {
      if(suclogin == -1) dynamicaddline("User initialization halted - false authentication by username " + tempuname + ".");
      if(global.maintmode)
      {
      dynamicaddline("User login attempt during maintenance mode: " + tempuname + ".");
      writeint(-2);
      }
      else writeint(-1);
      sendmessage(global.p_tcpsocket[pid]);
      uninit_user(pid);
      break;
      }
      if(global.p_online[pid]) sendmessage(global.p_tcpsocket[pid]);
    break;

    case 6:
      tempint = readint();
      //if(global.p_name[pid] == mb_getwriter(global.mb_topic[tempint,0])) Wouldn't work for moderators
      global.mb_topic[tempint,0] = readstring();
    break;

    case 7:
      tempint = readint();
      tempint2 = readint();
      //if(global.p_name[pid] == mb_getwriter(global.mb_topic[tempint,0])) Same deal as above
      global.mb_reply[tempint,tempint2] = readstring();
    break;
    
    case 8:
      global.iv_totalcats = readbyte();
      for(ivc = 0; ivc < global.iv_totalcats; ivc += 1)
      {
      if(ivc >= 32000) break;
      tempint = readint();
      uinv_set(global.p_uid[pid],ivc,0,/* = */0,tempint);
      for(ivi = 1; ivi <= tempint; ivi += 1)
      {
      if(ivi >= 32000) break;
      tempstr = readstring();
      uinv_set(global.p_uid[pid],ivc,ivi,/* = */1,tempstr);
      }
      }
    break;

    case 11:
      tempint = readint();
      global.mb_topic[global.mb_total[1],2] = tempint;
      global.mb_topic[global.mb_total[1],0] = readstring();
      global.mb_total[1] += 1;
      global.mbttopline = -1;
      mb_backup();
      //Make this board have a new post for everyone else.
      for(ipc = 0; ipc < global.u_total; ipc += 1)
      {
      if(ipc != global.p_uid[pid]) global.u_mb_newmsg[ipc,tempint] = 1;
      //dynamicaddline(global.u_name[ipc] + " get update for board " + string(tempint));
      }
      allupdate[6] = 1;
    break;

    case 12:
      tempint = readint();
      global.mb_reply[tempint,global.mb_topic[tempint,1]] = readstring();
      global.mb_topic[tempint,1] += 1;
      global.mbrtopline = global.mb_topic[tempint,1]-1;
      mb_trise(tempint);
      mb_backup();
      //Make this board have a new post for everyone else.
      tempint2 = global.mb_topic[tempint,2];
      for(ipc = 0; ipc < global.u_total; ipc += 1)
      {
      if(ipc != global.p_uid[pid])
      {
      global.u_mb_newmsg[ipc,tempint2] = 1;
      //dynamicaddline(global.u_name[ipc] + " get update for board " + string(tempint2));
      }
      }
      allupdate[6] = 1;
    break;

    case 13:
      tempint = readint(); //The board ID.
      clearbuffer();
      writebyte(13);
      writeint(global.mb_total[1]);
      writeint(tempint);
      tempint2 = 0;
      for(ru = 0; ru < global.mb_total[1]; ru += 1)
      {
        if(global.mb_topic[ru,2] == tempint)
        {
          tempint2 += 1;
          //show_message("Found Topic ID " + string(ru));
        }
      }
      writeint(tempint2); //Write the total number of topics in the board.
      //show_message("Total Topics " + string(tempint2));
      ru = 0;
      for(ru = 0; ru < global.mb_total[1]; ru += 1)
      {
        if(global.mb_topic[ru,2] == tempint)
        {
          writeint(ru); //Write topic ID.
          //show_message("Writing Topic ID " + string(ru));
          writestring(global.mb_topic[ru,0]);
          writeint(global.mb_topic[ru,1]);
          writeint(global.mb_topic[ru,2]);
        }
      }
      sendmessage(global.p_tcpsocket[pid]);
      //Mark this board as read by the user.
      global.u_mb_newmsg[global.p_uid[pid],tempint] = 0;
      clearbuffer();
      writebyte(19);
      for(ipc = 0; ipc < global.mb_total[0]; ipc += 1)
      {
      writebyte(global.u_mb_newmsg[global.p_uid[pid],ipc]);
      }
      sendmessage(global.p_tcpsocket[pid]);
      //dynamicaddline(global.p_name[pid] + " read board " + string(tempint));
    break;

    case 14:
      tempint = readint();
      clearbuffer();
      writebyte(14);
      writeint(tempint);
      writeint(global.mb_topic[tempint,1]);
      for(ru = 0; ru < global.mb_topic[tempint,1]; ru += 1)
      {
        writestring(global.mb_reply[tempint,ru]);
      }
      sendmessage(global.p_tcpsocket[pid]);
      //Mark this board as read by the user.
      tempint = global.mb_topic[tempint,2];
      global.u_mb_newmsg[global.p_uid[pid],tempint] = 0;
      clearbuffer();
      writebyte(19);
      for(ipc = 0; ipc < global.mb_total[0]; ipc += 1)
      {
      writebyte(global.u_mb_newmsg[global.p_uid[pid],ipc]);
      }
      sendmessage(global.p_tcpsocket[pid]);
    break;
    
    case 15:
      tempbyte = readbyte();
      tempbyte2 = readbyte();
      clearbuffer();
      writebyte(15);
      writebyte(pid);
      writebyte(tempbyte);
      sendmessage(global.p_tcpsocket[tempbyte2]);
      /*if(tempbyte == 0)
      {
        dynamicaddline("A summon request from " + global.p_name[pid] + " has been received for " + global.p_name[tempbyte2] + ".");
      }
      if(tempbyte == 1)
      {
        dynamicaddline("A join request from " + global.p_name[pid] + " has been received for " + global.p_name[tempbyte2] + ".");
      }*/
    break;
    
    case 16:
      global.p_area[pid] = readstring();
      tempbyte = readbyte();
      clearbuffer();
      writebyte(16);
      writebyte(pid);
      writestring(global.p_area[pid]);
      sendmessage(global.p_tcpsocket[tempbyte]);
    break;
    
    case 18:
      clearbuffer();
      writebyte(18);
      writeint(global.mb_total[0]);
      writeint(global.mb_total[1]);
      for(i = 0; i < global.mb_total[0]; i += 1)
      {
        writestring(global.mb_board[i]);
      }
      sendmessage(global.p_tcpsocket[pid]);
    break;
    
    case 19:
      bytetemp = readbyte();
      if(!bytetemp) mb_tcollapse(readint());
      else mb_rcollapse(readint(),readint());
      mb_backup();
    break;
    
    case 20:
      //Mod Data Commands
      //0 - Execute code
      //1 - Variable query
      //2 - Set UID target
      //3 - Send code to UID
      //4 - All-player query
      bytetemp = readbyte();
      if(bytetemp < 4) tempstring = readstring();
      if(bytetemp == 0)
      {
        execute_string(tempstring);
        caddline(global.p_name[pid] + " had the server execute code: " + tempstring);
      }
      else if(bytetemp == 1)
      {
        execute_string("tempint = " + tempstring + ";");
        tempstring = "[s] " + tempstring + " = " + string(tempint);
        clearbuffer();
        writebyte(12);
        writestring("caddline('"+tempstring+"',global.c_server);");
        sendmessage(global.p_tcpsocket[pid]);
        caddline(global.p_name[pid] + " requested a variable query for " + tempstring + ".");
      }
      else if(bytetemp == 2)
      {
        global.mod_uid = real(tempstring);
        caddline(global.p_name[pid] + " set the targeted uid to " + tempstring);
      }
      else if(bytetemp == 3 && global.p_online[global.u_pid[global.mod_uid]])
      {
        clearbuffer();
        writebyte(12);
        writestring(tempstring);
        sendmessage(global.p_tcpsocket[global.u_pid[global.mod_uid]]);
        caddline(global.p_name[pid] + " sent the following code to uid " + string(global.mod_uid) + ": " + tempstring);
      }
      else if(bytetemp == 4)
      {
        tempstring = "Online:  ";
        for(tempint = 0; tempint < global.pindex; tempint += 1)
        {
          if(global.p_online[tempint])
            tempstring += "(" + string(global.p_uid[tempint]) + ") " + global.p_name[tempint] +
            " [Room " + string(global.p_room[tempint]) + "]   ";
        }
        clearbuffer();
        writebyte(12);
        writestring("caddline('"+tempstring+"',global.c_server);");
        sendmessage(global.p_tcpsocket[pid]);
        caddline(global.p_name[pid] + " requested an online player query.");
      }
    break;
    
    case 21:
      global.hxbridge[global.p_uid[pid],0] = readint();
      for(i = 1; i <= global.hxbridge[global.p_uid[pid],0]; i += 1) global.hxbridge[global.p_uid[pid],i] = readstring();
    break;
    
    case 22:
      bytetemp = readbyte();
      bytetemp2 = readbyte();
      bytetemp3 = 0;
      if(bytetemp == 0)
        global.p_canduo[pid] = bytetemp2;
      else if(bytetemp == 1)
        global.p_induo[pid] = bytetemp2;
      else if(bytetemp == 2)
      {
        global.p_watchable[pid] = bytetemp2;
        if(!bytetemp2) bytetemp3 = readbyte();
      }
      allupdate[7] = 1;
    break;
    
    case 23:
      bytetemp = readbyte();
      clearbuffer();
      writebyte(23);
      writebyte(pid);
      sendmessage(global.p_tcpsocket[bytetemp]);
    break;
    
    
    /*case 24:
      caddline(global.p_name[pid] + " has logged out.");
      allupdate[5] = pid+1;
      uninit_user(pid,global.p_tcpsocket[pid]);
    break;*/
    
    case 26:
      global.p_logalarm[pid] = 2700;
    break;
  }

//Update all other players that this player matters to:
if(global.pnum > 0)
{
for(up = 0; up < global.pindex; up += 1)
{
    if(up != pid && up != tpid && global.p_online[up])
    {
        //All:
        if(allupdate[0])
        {
            clearbuffer();
            writebyte(0);
            writebyte(pid);
            writebyte(global.p_uid[pid]);
            writestring(global.p_name[pid]);
            sendmessage(global.p_tcpsocket[up]);
            //caddline("Sent player log-on message of "+global.p_name[pid]+" to "+global.p_name[up]+".");
        }
        if(allupdate[1])
        {
            clearbuffer();
            writebyte(1);
            writebyte(pid);
            writeint(global.p_room[pid]);
            sendmessage(global.p_tcpsocket[up]);
        }
        if(allupdate[5] > 0)
        {
            clearbuffer();
            writebyte(5);
            writebyte(allupdate[5]-1);
            sendmessage(global.p_tcpsocket[up]);
        }
        if(allupdate[6])
        {
            clearbuffer();
            writebyte(19);
            for(ipc = 0; ipc < global.mb_total[0]; ipc += 1)
            {
            writebyte(global.u_mb_newmsg[global.p_uid[up],ipc]);
            }
            sendmessage(global.p_tcpsocket[up]);
        }
        if(allupdate[7])
        {
            clearbuffer();
            writebyte(22);
            writebyte(pid);
            writebyte(bytetemp);
            writebyte(bytetemp2);
            writebyte(bytetemp3);
            sendmessage(global.p_tcpsocket[up]);
        }

        //All in same room:
        if(pid == -1) cpid = tpid;
        else cpid = pid;
        
        if(global.p_room[cpid] == global.p_room[up])
        {
        if(allupdate[2])
        {
            clearbuffer();
            writebyte(2);
            writebyte(cpid);
            writeint(global.p_spr[cpid,0]);
            for(tempint = 1; tempint <= 5; tempint += 1)
            {
            writestring(global.p_spr[cpid,tempint]);
            }
            if(global.p_tcprec[up]) sendmessage(global.p_tcpsocket[up]);
            else sendmessage(global.playerudpsocket, global.p_ip[up], global.playerudpport);
        }
        if(allupdate[3])
        {
            clearbuffer();
            writebyte(3);
            writebyte(cpid);
            writeint(global.p_x[cpid]);
            writeint(global.p_y[cpid]);
            writebyte(global.p_fps[cpid]);
            writefloat(global.p_xdif[cpid]);
            writefloat(global.p_ydif[cpid]);
            if(global.p_tcprec[up]) sendmessage(global.p_tcpsocket[up]);
            else sendmessage(global.playerudpsocket, global.p_ip[up], global.playerudpport);
        }
        if(allupdate[4])
        {
            if(global.p_room[pid] == global.p_room[up] && abs(global.p_x[pid]-global.p_x[up]) <= 480 && abs(global.p_y[pid]-global.p_y[up]) <= 360)
            {
            clearbuffer();
            writebyte(4);
            writebyte(pid);
            writebyte(0);
            writestring(strtemp);
            sendmessage(global.p_tcpsocket[up]);
            }
        }
        }
    }
}
for(tempint = 0; tempint <= 6; tempint += 1) allupdate[tempint] = 0;
}
//End mass update.
    
/*tempint = 0;
for(tempint2 = 0; tempint2 < global.pindex; tempint2 += 1)
{
if(global.p_online[tempint2]) tempint = tempint2;
}
if(pid == tempint) break;*/
loopcount += 1;

}


}