public static ByteBuffer constructStreamHeader(boolean compress, boolean stream) {
    /*
    Setting up the protocol header. This is 4 bytes long
    represented as an integer. The first 2 bits indicate
    the serializer type. The 3rd bit indicates if compression
    is turned on or off. It is turned off by default. The 4th
    bit indicates if we are in streaming mode. It is turned off
    by default. The following 4 bits are reserved for future use.
    The next 8 bits indicate a version number. Remaining 15 bits
    are not used currently.
    */
    int n = 0;
    // Setting up the serializer bit
    n |= serializerType_.ordinal();
    // set compression bit.
    if (compress) n |= 4;

    // set streaming bit
    if (stream) n |= 8;

    // Setting up the version bit
    n |= (version_ << 8);
    /* Finished the protocol header setup */

    byte[] header = toByteArray(n);
    ByteBuffer buffer = ByteBuffer.allocate(16 + header.length);
    buffer.put(protocol_);
    buffer.put(header);
    buffer.flip();
    return buffer;
  }
  public static ByteBuffer packIt(
      byte[] bytes, boolean compress, boolean stream, boolean listening) {
    byte[] size = toByteArray(bytes.length);
    /*
         Setting up the protocol header. This is 4 bytes long
         represented as an integer. The first 2 bits indicate
         the serializer type. The 3rd bit indicates if compression
         is turned on or off. It is turned off by default. The 4th
         bit indicates if we are in streaming mode. It is turned off
         by default. The 5th bit is used to indicate that the sender
         is not listening on any well defined port. This implies the
         receiver needs to cache the connection using the port on the
         socket. The following 3 bits are reserved for future use.
         The next 8 bits indicate a version number. Remaining 15 bits
         are not used currently.
    */
    int n = 0;
    // Setting up the serializer bit
    n |= serializerType_.ordinal();
    // set compression bit.
    if (compress) n |= 4;

    // set streaming bit
    if (stream) n |= 8;

    // set listening 5th bit
    if (listening) n |= 16;

    // Setting up the version bit
    n |= (version_ << 8);
    /* Finished the protocol header setup */

    byte[] header = toByteArray(n);
    ByteBuffer buffer = ByteBuffer.allocate(16 + header.length + size.length + bytes.length);
    buffer.put(protocol_);
    buffer.put(header);
    buffer.put(size);
    buffer.put(bytes);
    buffer.flip();
    return buffer;
  }