open connection only when necessary