Adding safeClose() methods to cleanup stream/socket closing