code: private void handleKey(Selector selector, SelectionKey key) throws IOException {
          if (key.isValid() && key.isAcceptable()) {
              // Accept a new connection, give it a stream connection as an attachment
              SocketChannel newChannel = sc.accept();
              newChannel.configureBlocking(false);
              SelectionKey newKey = newChannel.register(selector, SelectionKey.OP_READ);
              try {
                  ConnectionHandler handler = new ConnectionHandler(connectionFactory, newKey);
                  newKey.attach(handler);
                  handler.connection.connectionOpened();
              } catch (IOException e) {
                  // This can happen if ConnectionHandler's call to get a new handler returned null
                  log.error("Error handling new connection", Throwables.getRootCause(e).getMessage());
                  newKey.channel().close();
              }
          } else { // Got a closing channel or a channel to a client connection
              ConnectionHandler.handleKey(key);
          }
      }
docs: Handle a SelectionKey which was selected