I have this code for years, but never shared it. Giving back this to community.
Some obscure bugs fixed, code comments clarified, and error checks added.
You can try out the chatbox live on a Drupal 6 site here: http://permutations.com/drup/
Drupal 6 backport
Drupal is a registered trademark of Dries Buytaert.