remove all the close(sd) now that you're using the socket closer. Otherwise you'll close the same socket twice