private void close(SelectionKey key) {
    Transmission transmission = getTransmission(key);
    if (transmission != null) {
      logger.debug("Closing connection from {}", transmission.getConnectionId());
      closedConnections.add(transmission.getConnectionId());
      keyMap.remove(transmission.getConnectionId());
      numActiveConnections.set(keyMap.size());
      unreadyConnections.remove(transmission.getConnectionId());
      try {
        transmission.close();
      } catch (IOException e) {
        logger.error("IOException thrown during closing of transmission with connectionId {} :",
            transmission.getConnectionId(), e);
      }
    } else {
      key.attach(null);
      key.cancel();
      SocketAddress address = null;
      try {
        SocketChannel socketChannel = channel(key);
        address = socketChannel.socket().getRemoteSocketAddress();
        socketChannel.socket().close();
        socketChannel.close();
      } catch (IOException e) {
        metrics.selectorCloseSocketErrorCount.inc();
        logger.error("Exception closing connection to remote host {} :", address, e);
      }
    }
    this.metrics.selectorConnectionClosed.inc();
  }
Begin closing this connection by given key
ambry-network/src/main/java/com/github/ambry/network/Selector.java