Fixed a bug when closing connections