@Override
	public void mainloop() {
		while (true) {
			try {
				selector.select();
				Iterator<?> selectedKeys = this.selector.selectedKeys().iterator();

				while (selectedKeys.hasNext()) {

					SelectionKey key = (SelectionKey) selectedKeys.next();
					selectedKeys.remove();

					if (!key.isValid()) {
						System.out.println("The key is not valid");
					} else if (key.isConnectable()) {
						handleConnection(key);
					} else if (key.isReadable()) {
						handleRead(key);
					} else if (key.isWritable()) {
						handleWrite(key);
					} else if (key.isAcceptable()) {
						handleAccept(key);
					} else {
						System.out.println("Unknown key");
					}
				}
			} catch (Exception e) {
                e.printStackTrace();
				panic("Error during the selection of a key");
			}
		}
	}