state thread, fork from http://state-threads.sourceforge.net/