Close connections safely