Added support to close the TCP socket correctly