Use counter instead of caching connections.

Fix semaphore acquire/release.