/** The main processing loop for incoming RTSP messages. */
  public void run() {

    ByteArrayOutputStream baos = new ByteArrayOutputStream();

    int ch0 = 0;
    int ch1 = 0;
    int ch2 = 0;
    int ch3 = 0;

    while (connectionIsAlive) {
      try {
        ch3 = ch2;
        ch2 = ch1;
        ch1 = ch0;
        ch0 = is.read();

        if (-1 != ch0) {

          baos.write(ch0);

          if ('\r' == ch1 && '\n' == ch0 && '\r' == ch3 && '\n' == ch2) {

            // message header is completely received

            String header = new String(baos.toByteArray());

            int content_length = getContentLength(header);

            for (int i = 0; i < content_length; i++) {

              ch0 = is.read();

              if (-1 != ch0) {
                baos.write(ch0);
              } else {
                connectionIsAlive = false;
                break;
              }
            }

            // whole message is completely received
            ds.processIncomingMessage(baos.toByteArray());
            baos.reset();
          }
        } else {
          connectionIsAlive = false;
        }

      } catch (Exception e) {
        connectionIsAlive = false;
      }
    }
  }
  /**
   * Creates a new RTSP connection.
   *
   * @param url RTSP URL object
   * @exception IOException Throws and IOException if a connection to the RTSP server cannot be
   *     established.
   */
  public RtspConnectionBase(RtspDS ds) throws IOException {

    try {
      openStreams(ds.getUrl());
    } catch (IOException e) {
      throw e;
    }

    if (null == is) throw new IOException("InputStream creation failed");
    if (null == os) throw new IOException("OutputStream creation failed");

    connectionIsAlive = true;
    this.ds = ds;

    start();
  }