Added closing of socket and working writing of received message.