Rebase of #489 "fix error in EM::Client::close"

Rebase of #489 "fix error in EM::Client::close" by @mrms-dos.