Move socket close to per instance

* REALLY not happy i have to manually close these.
* Wrap it with a connection check for safety reasons.
* Try not to write code when really tired and the CR person is on
  holiday
Merge pull request #188 from jrconlin/master

Move socket close to per instance