private synchronized Event read_event_from_stream () {

    int available = 0;
    try {
      available = in.available ();
      //System.err.println("available: " + available);
    } catch (IOException ex) {
      handle_exception (ex);
    }

    if (available == 0)
      return null;

    // We want to look-ahead the first byte to determine the type of the
    // response.
    int code = -1;
    try {
      in.mark (1);
      code = read_int8 ();
      in.reset ();
    } catch (IOException ex) {
      ex.printStackTrace();
    }
    //System.err.println("reading code: " + code + " masked: " + (code & 0x7f));
    code = code & 0x7f; // Remove synthetic mask.
    Event ev = null;
    if (code >= 64 && code <= 127)
      ev = read_extension_event (code);
    else
      ev = read_core_event (code);
    return ev;
  }