Use socket.shutdownInput() instead of inputStream.close() to avoid inadvertently closing the socket.
Simlarly for socket.shutdownOutput().
Implement Closeable on TcpURLConnection to close the socket.
Refactor and apply checkstyle rules.