@Nullable
  ProtocolFrame waitForResponse(final int sequence, long timeout) {
    ProtocolFrame response;
    long until = System.currentTimeMillis() + timeout;

    synchronized (myResponseQueue) {
      boolean interrupted = false;
      do {
        try {
          myResponseQueue.wait(1000);
        }
        catch (InterruptedException e) {
          // restore interrupted flag
          Thread.currentThread().interrupt();

          interrupted = true;
        }
        response = myResponseQueue.get(sequence);
      }
      while (response == null && shouldWaitForResponse() && !interrupted && System.currentTimeMillis() < until);
      myResponseQueue.remove(sequence);
    }

    return response;
  }
@Nullable
  ProtocolFrame waitForResponse(final int sequence, long timeout)