I normally log out just by using the paperdoll gump and clicking "logout" if I'm going to be logging back in. However if you are looking at trying to manage logging back in, then you are looking at a someone complex script since debugging login subs can be troubling.
Sometimes the best explanation is looking at code, so Toptwo's suggestion to look at the fisherman is a good one. I created criterion that would cause you to log out. So if you reach a given online time, bank run count, or MIBs fished up, you'll transport to an inn and close the client.