Refactor Connection class to properly support attach api

Also stop thread leak when the number of metrics is higher than the
threshold.