Re-factor connection detection to utilise TCPSocket.new, etc.

Signed-off-by: Krzysztof Wilczynski <krzysztof.wilczynski@linux.com>