fixed conenction not closed issue

connection wasn't closed if it was successful. Added finally to the try to close it if it exists.