@Override
	public void run() {
		try {
			// create code from private IP address: fourth octet
			String localIP = Inet4Address.getLocalHost().getHostAddress();
			System.out.println("Server: " + localIP);
			String[] ipArray = localIP.split("\\.");
			System.out.println("Code: " + ipArray[3]);

			System.out
					.println("Binding to port " + port + ", please wait  ...");

			if (server == null) {
				// opens port, so client can connect
				server = new ServerSocket(port);
			}

			System.out.println("Server started: " + server);
			System.out.println("Waiting for a client ...");

			// block until client connects
			socket = server.accept();
			System.out.println("Client accepted: " + socket);
			socketList.add(socket);

			// create next server for next client
			new Server(port, server, socketList).start();

			// opens the socket to the client for reading. It blocks!
			open();

			boolean done = false;

			// main loop, keep going until .bye was sent
			while (!done) {

				// everyone is supposed to get the message
				boolean allReceive = true;
				try {
					String line = streamIn.readUTF(); // read data from client
					System.out.println(line); // write data to server's console

					switch (line) {
					case "client_amount":
						// client wants to know the amount of clients currently
						// connected
						line = "client_amount:" + socketList.size();
						allReceive = false;
						break;
					case ".bye":
						line = "A user left the server.";
						break;
					case ".kill":
						line = "Sorry, the server shut down.\nPlease close the program.";
						break;
					}

					if (allReceive) {
						// all clients are going to receive the message except
						// for the one sending it
						sendAll(line);
					} else {
						// just the client sending the line will receive this
						DataOutputStream streamOut = new DataOutputStream(
								socket.getOutputStream());

						// send data to client
						streamOut.writeUTF(line);

						// make sure all data has been sent
						streamOut.flush();
					}

					// check if we need to exit this socket
					done = line.equals(".bye");

					// check if we need to exit all
					if (line.equals(".kill")) {
						System.exit(0);
					}
				} catch (IOException ioe) {
					done = true;
				}
			}

			// remove this socket from the list
			socketList.remove(socket);
			close();
		} catch (IOException ioe) {
			System.out.println(ioe);
		}
	}