Fix for segfault when closing the connection