Fix socket and file code to close everytime it is finished being processed