ruby telnet question

Is there a way to implement a logout on any error that comes up while doing a telnet? I have eqpt that gets a bit crazy with cli sessions left hung up on the server.