backport r48265 without to using refinements

I extracted patch for connection leak from https://github.com/ruby/ruby/commit/f6616071dc4089b772d3dcb6c1031653fb4d2054 . original patch used ```refinements```. but rubygems still support Ruby 1.8 and 1.9.

this pull request fixes connection leak on all versions of Ruby.