
server = new Server(1337);

clients = [];
while (true)
{
	// Check for new connections
	clientSocket = server.Accept();
	if (clientSocket != 0)
	{
		var i = clients.length;
		clients[i] = new Object();
		clients[i].Connection = clientSocket;
		clients[i].Nick = undefined;
	}
	
	// Process messages
	for (var i = 0; i < clients.length; i++)
	{
		var say = clients[i].Connection.Receive();
		
		if (say != undefined)
		{
			if (clients[i].Nick == undefined)
			{
				clients[i].Nick = say;
				say = "*** " + say + " has joined the chat!\n";
				Print("New client connected: " + clients[i].Nick + "\n");
			}
			else
			{
				Print(say);
			}
		
			for (var n = 0; n < clients.length; n++)
			{
				clients[n].Connection.Send(say);
			}
		}
	}
	
	Sleep(1);		
}

