private void receiveMessages() throws IOException {
    // handshake (true) endpoint versions
    DataOutputStream out = new DataOutputStream(socket.getOutputStream());
    // if this version is < the MS version the other node is trying
    // to connect with, the other node will disconnect
    out.writeInt(MessagingService.current_version);
    out.flush();
    DataInputStream in = new DataInputStream(socket.getInputStream());
    int maxVersion = in.readInt();
    // outbound side will reconnect if necessary to upgrade version
    assert version <= MessagingService.current_version;
    from = CompactEndpointSerializationHelper.deserialize(in);
    // record the (true) version of the endpoint
    MessagingService.instance().setVersion(from, maxVersion);
    logger.debug(
        "Set version for {} to {} (will use {})",
        from,
        maxVersion,
        MessagingService.instance().getVersion(from));

    if (compressed) {
      logger.debug("Upgrading incoming connection to be compressed");
      in = new DataInputStream(new SnappyInputStream(socket.getInputStream()));
    } else {
      in = new DataInputStream(new BufferedInputStream(socket.getInputStream(), 4096));
    }

    while (true) {
      MessagingService.validateMagic(in.readInt());
      receiveMessage(in, version);
    }
  }
  private void receiveMessages() throws IOException {
    // handshake (true) endpoint versions
    DataOutputStream out = new DataOutputStream(socket.getOutputStream());
    out.writeInt(MessagingService.current_version);
    out.flush();
    DataInputStream in = new DataInputStream(socket.getInputStream());
    int maxVersion = in.readInt();

    from = CompactEndpointSerializationHelper.deserialize(in);
    // record the (true) version of the endpoint
    MessagingService.instance().setVersion(from, maxVersion);
    logger.debug(
        "Set version for {} to {} (will use {})",
        from,
        maxVersion,
        MessagingService.instance().getVersion(from));

    if (compressed) {
      logger.debug("Upgrading incoming connection to be compressed");
      if (version < MessagingService.VERSION_21) {
        in = new DataInputStream(new SnappyInputStream(socket.getInputStream()));
      } else {
        LZ4FastDecompressor decompressor = LZ4Factory.fastestInstance().fastDecompressor();
        Checksum checksum =
            XXHashFactory.fastestInstance()
                .newStreamingHash32(OutboundTcpConnection.LZ4_HASH_SEED)
                .asChecksum();
        in =
            new DataInputStream(
                new LZ4BlockInputStream(socket.getInputStream(), decompressor, checksum));
      }
    } else {
      in = new DataInputStream(new BufferedInputStream(socket.getInputStream(), BUFFER_SIZE));
    }

    if (version > MessagingService.current_version) {
      // save the endpoint so gossip will reconnect to it
      Gossiper.instance.addSavedEndpoint(from);
      logger.info("Received messages from newer protocol version {}. Ignoring", version);
      return;
    }
    // outbound side will reconnect if necessary to upgrade version

    while (true) {
      MessagingService.validateMagic(in.readInt());
      receiveMessage(in, version);
    }
  }