code extracted and reftactored (but not tested) from autobahn/stores/remote