public CompletableFuture<PeerConnection> connect(final Peer peer) {
    // Check if we're ready to establish connections
    if (!localNode.isReady()) {
      return CompletableFuture.failedFuture(
          new IllegalStateException(
              "Cannot connect before "
                  + this.getClass().getSimpleName()
                  + " has finished starting"));
    }

    // Check peer is valid
    final EnodeURL enode = peer.getEnodeURL();
    if (!enode.isListening()) {
      final String errorMsg = "Attempt to connect to peer with no listening port: " + enode;
      LOG.warn(errorMsg);
      return CompletableFuture.failedFuture((new IllegalArgumentException(errorMsg)));
    }

    // Check permissions
    if (!peerPermissions.allowNewOutboundConnectionTo(peer)) {
      return CompletableFuture.failedFuture(peerPermissions.newOutboundConnectionException(peer));
    }

    final CompletableFuture<PeerConnection> peerConnectionCompletableFuture;
    if (checkWhetherToConnect(peer, false)) {
      try {
        synchronized (this) {
          peerConnectionCompletableFuture =
              peersConnectingCache.get(
                  peer.getId(), () -> createPeerConnectionCompletableFuture(peer));
        }
      } catch (final ExecutionException e) {
        throw new RuntimeException(e);
      }
    } else {
      final String errorMsg =
          "None of the ProtocolManagers wants to connect to peer " + peer.getId();
      LOG.trace(errorMsg);
      return CompletableFuture.failedFuture((new RuntimeException(errorMsg)));
    }

    return peerConnectionCompletableFuture;
  }
Connect to the peer

@param peer The peer to connect to
@return A future that will resolve to the existing or newly-established connection with this
    peer.
ethereum/p2p/src/main/java/org/hyperledger/besu/ethereum/p2p/rlpx/RlpxAgent.java