Move the socket initialization to outer scope.

This guarentees that socket is not null. Additionally remove the second `socket.close()` in the `DumpTask`.