code: private static OutputStream getOutputStream(final Socket socket) throws IOException {
      final OutputStream delegate = socket.getOutputStream();
      return new OutputStream() {
  
        @Override public void close() throws IOException {
          delegate.flush();
          synchronized (socket) {
            socket.shutdownOutput();
            if (socket.isInputShutdown()) {
              socket.close();
            }
          }
        }
      
        // Boring delegates from here on down
        @Override public void write(int b) throws IOException {
          delegate.write(b);
        }
      
        @Override public void write(byte[] b) throws IOException {
          delegate.write(b);
        }
      
        @Override public void write(byte[] b, int off, int len) throws IOException {
          delegate.write(b, off, len);
        }
      
        @Override public void flush() throws IOException {
          delegate.flush();
        }
      };
    }
docs: Returns an {@link OutputStream} for the socket, but unlike {@link Socket#getOutputStream()}
  when you call {@link OutputStream#close() close} it only closes the output end of the socket
  instead of the entire socket.