public void close() throws IOException {
   synchronized (closeLock) {
     if (closed) {
       return;
     }
     socketOutput.close();
     socketInput.close();
     socket.close();
     closed = true;
   }
 }
  public void writePacket(byte b[]) throws IOException {
    if (!isOpen()) {
      throw new ClosedConnectionException("connection is closed");
    }

    /*
     * Check the packet size
     */
    if (b.length < 11) {
      throw new IllegalArgumentException("packet is insufficient size");
    }
    int b0 = b[0] & 0xff;
    int b1 = b[1] & 0xff;
    int b2 = b[2] & 0xff;
    int b3 = b[3] & 0xff;
    int len = ((b0 << 24) | (b1 << 16) | (b2 << 8) | (b3 << 0));
    if (len < 11) {
      throw new IllegalArgumentException("packet is insufficient size");
    }

    /*
     * Check that the byte array contains the complete packet
     */
    if (len > b.length) {
      throw new IllegalArgumentException("length mis-match");
    }

    synchronized (sendLock) {
      try {
        /*
         * Send the packet (ignoring any bytes that follow
         * the packet in the byte array).
         */
        socketOutput.write(b, 0, len);
      } catch (IOException ioe) {
        if (!isOpen()) {
          throw new ClosedConnectionException("connection is closed");
        } else {
          throw ioe;
        }
      }
    }
  }