code: protected int readBytes(Socket s, byte[] b) throws Exception {
      int bytesRead = 0;
      int bytesExpected = b.length;
      InputStream is = s.getInputStream();
      while (bytesRead < bytesExpected) {
        int n = is.read(b, bytesRead, bytesExpected - bytesRead);
        if (n < 0) {
          return n;
        }
        bytesRead += n;
      }
  
      return bytesRead;
    }
docs: -------