Implement proper socket closing.