public Event read_event () {

    //System.err.println("read_event");
    //System.err.println("read event from stream");

    // Otherwise we read and return the first event from the stream.
    Event ev = null;
    do {
      // If there are any events already queued up, then return the first
      // event in the queue.
      if (events.size () > 0) {
        ev = (Event) events.removeFirst ();
      } else {
        ev = read_event_from_stream ();
      }

      // If this returned null, there's a reply in the response stream and
      // some other thread is waiting for it, or there is no event and we keep
      // waiting for one...
      if (ev == null) {
        try {Thread.sleep (40); } catch (Exception ex) {}
        //Thread.yield ();
      }

    } while (ev == null);
//    System.err.println("event: " + ev);
    return ev;
  }