removing memory allocations errors.

trying to close of sockets correctly and trying to create more consistency.