Fixed some bugs reffering to the closing of a tcp socket