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);
    }
  }
  /*
      Use this version for fire and forget style messaging.
  */
  public void sendOneWay(Message message, EndPoint to) {
    // do local deliveries
    if (message.getFrom().equals(to)) {
      MessagingService.receive(message);
      return;
    }

    Runnable tcpWriteEvent = new MessageSerializationTask(message, to);
    messageSerializerExecutor_.execute(tcpWriteEvent);
  }
  public void sendUdpOneWay(Message message, EndPoint to) {
    EndPoint from = message.getFrom();
    if (message.getFrom().equals(to)) {
      MessagingService.receive(message);
      return;
    }

    UdpConnection connection = null;
    try {
      connection = new UdpConnection();
      connection.init();
      connection.write(message, to);
    } catch (IOException e) {
      logger_.warn(LogUtil.throwableToString(e));
    } finally {
      if (connection != null) connection.close();
    }
  }
  private InetAddress receiveMessage(DataInputStream input, int version) throws IOException {
    int id;
    if (version < MessagingService.VERSION_20) id = Integer.parseInt(input.readUTF());
    else id = input.readInt();

    long timestamp = System.currentTimeMillis();
    // make sure to readInt, even if cross_node_to is not enabled
    int partial = input.readInt();
    if (DatabaseDescriptor.hasCrossNodeTimeout())
      timestamp = (timestamp & 0xFFFFFFFF00000000L) | (((partial & 0xFFFFFFFFL) << 2) >> 2);

    MessageIn message = MessageIn.read(input, version, id);
    if (message == null) {
      // callback expired; nothing to do
      return null;
    }
    if (version <= MessagingService.current_version) {
      MessagingService.instance().receive(message, id, timestamp);
    } else {
      logger.debug("Received connection from newer protocol version {}. Ignoring message", version);
    }
    return message.from;
  }