Minor fixes

Fixed a bug with the output.
Small additions and fixes

Added the code to receive the reply and wait before close the sockets
and fixed a minor bug with the output.