Improved the closing of socket handles, making it more consistent.
Merge remote-tracking branch 'origin/master'