Adding a check to avoid printing error messages when closing the
connection