import java.io.IOException;
import java.net.ServerSocket;


public class ChatServer {
  private int port;
  private ServerSocket listenSocket;

  public ChatServer(int pPort) {
    this.port = pPort;
  }

  public void runServer() throws IOException {
    try {
      listenSocket = new ServerSocket(port);
    } catch (IOException e) {
      System.out.println("Error opening socket on port " + port + ", failed to start server");
      throw e;
    }

    // TODO: add server shutdown control
    boolean running = true;
    while (running) {
      try {
        listenSocket.accept();
      } catch (IOException e) {
        e.printStackTrace();
        System.out.println("Error accepting new connection");
      }
    }

    try {
      listenSocket.close();
    } catch (IOException e) {
      System.out.println("Error closing socket on port " + port + " during stop server");
      throw e;
    }
  }

  private static void printUsage() {
    System.out.println("Usage: ChatServer [port]");
  }

  private static int startServer(int pPort) {
    int exitCode = 0;
    ChatServer server = new ChatServer(pPort);
    try {
      server.runServer();
    } catch (IOException e) {
      e.printStackTrace();
      exitCode = 2;
    }
    return exitCode;
  }

  public static void main(String[] args) {
    if (args.length == 0) {
      printUsage();
      System.exit(1);
    }

    int port = Integer.parseInt(args[0]);

    System.exit(startServer(port));
  }

}

@@ -37,28 +37,119 @@
 ava.net.
-ServerSocket
+InetSocketAddress;%0Aimport java.nio.channels.ServerSocketChannel;%0Aimport java.nio.channels.SocketChannel
 ;%0A%0A%0Apubl
@@ -204,33 +204,26 @@
 ate 
-ServerSocket listenSocket
+AllClients clients
 ;%0A%0A 
@@ -277,16 +277,48 @@
  pPort;%0A
+    clients = new AllClients();%0A
   %7D%0A%0A  p
@@ -369,57 +369,190 @@
     
-try %7B%0A      listenSocket = new ServerSocket
+ServerSocketChannel serverSocketChannel = null;%0A    try %7B%0A      serverSocketChannel = ServerSocketChannel.open();%0A      serverSocketChannel.socket().bind(new InetSocketAddress
 (port)
+)
 ;%0A  
@@ -811,28 +811,240 @@
     
-listenSocket.accept(
+SocketChannel socketChannel =%0A            serverSocketChannel.accept();%0A        Client newClient = new Client(socketChannel);%0A        newClient.setUsername(clients.generateRandomUniqueUsername());%0A        clients.addClient(newClient
 );%0A 
@@ -1200,20 +1200,27 @@
     
-listenSocket
+serverSocketChannel
 .clo

Change ChatServer to use java.nio instead of java.io, create Client on accept