// A simple Chat server on port 1234

function chatServer() {
  var tcp = new Socket;
  // listen on port 1234
  writeln ("Chat server listening on port 1234");
  if (tcp.listen (1234)) {
    for (;;) {
      // poll for a new connection
      var connection = tcp.poll();
      if (connection != null) {
        writeln ("Connection from " + connection.host);
        // we have a new connection, so welcome and chat
        // until client terminates the session
        connection.writeln ("Welcome to a little chat!");
        chat (connection);
        connection.writeln ( "*** Goodbye ***");
        connection.close();
        delete connection;
        writeln ("Connection closed");
      }
    }
  }
}
