You are using /write inside the on sockread event, each time you call /write it has to open the file, write the content and then closr the file.
Whereas you could open the file once, write the data, and close it once inside the on sockclose event, this is most likely the issue here.