Problem with using form buttons to do actions is the connection will be closed once the Apache process is done sending data to the browser. Thus you'd have to open a new socket each time you want to do something in that sort of model.
You would need to get change your fsockopen to pfsockopen
pfsockopen() works exactly like fsockopen but the connection is not closed once the script finishes executing. You need to make sure you have limits and the enviroment well defined or you could run into some serious problems.