public void receiveSelectResult(P2PSocket<Identifier> socket, boolean canRead, boolean canWrite)
     throws IOException {
   // TODO: Optimization: Check array at each step, to fail faster
   // TODO: Make timeout/cancellable
   if (canWrite) throw new IOException("Never asked to write!");
   if (!canRead) throw new IOException("Can't read!");
   long bytesRead;
   if ((bytesRead = socket.read(buf)) < 0) {
     socket.close();
     return;
   }
   notifyListenersRead(
       (int) bytesRead, socket.getIdentifier(), socket.getOptions(), false, true);
   if (buf.hasRemaining()) {
     socket.register(true, false, this);
   } else {
     if (Arrays.equals(HEADER, buf.array())) {
       // header matched
       cancel();
       callback.incomingSocket(socket);
     } else {
       cancel();
       errorHandler.receivedUnexpectedData(socket.getIdentifier(), buf.array(), 0, null);
       socket.close();
     }
   }
 }
 @Override
 public void run() {
   socket.close();
   errorHandler.receivedException(
       socket.getIdentifier(),
       new StalledSocketException(
           socket.getIdentifier(), "Timeout on incoming socket expired."));
 }