/** Read the incoming stream until it ends. */
  private void readStream() throws Exception {
    while (!shutdown) {
      Element doc = reader.parseDocument().getRootElement();

      if (doc == null) {
        // Stop reading the stream since the server has sent an end of stream element and
        // probably closed the connection
        return;
      }

      Packet packet;
      String tag = doc.getName();
      if ("message".equals(tag)) {
        packet = new Message(doc);
      } else if ("presence".equals(tag)) {
        packet = new Presence(doc);
      } else if ("iq".equals(tag)) {
        packet = getIQ(doc);
      } else {
        throw new XmlPullParserException("Unknown packet type was read: " + tag);
      }
      // Request the component to process the received packet
      component.processPacket(packet);
    }
  }
 /**
  * A dedicated thread loop for reading the stream and sending incoming packets to the appropriate
  * router.
  */
 public void run() {
   try {
     readStream();
   } catch (EOFException eof) {
     // Normal disconnect
   } catch (SocketException se) {
     // Do nothing if the exception occured while shutting down the component otherwise
     // log the error and try to establish a new connection
     if (!shutdown) {
       component.getManager().getLog().error(se);
       component.connectionLost();
     }
   } catch (XmlPullParserException ie) {
     component.getManager().getLog().error(ie);
   } catch (Exception e) {
     component.getManager().getLog().warn(e);
   }
 }