detect_ip: avoid ResourceWarning about unclosed socket on python 3.x

i just added try/finally to support s.close(), rest is just block indentation change.