Refactoring socket.outputStream usage