Code from http://jsocks.cvs.sourceforge.net/viewvc/jsocks/