1) $ggg is not the same (in some cases it is created randomly) for all invocation in the original code, however for debugging I changed it to a static name, and the error stays the same.
2) Yes, sysopen() fails consistently.
3) I tried it 100++ times.
4) Yes, the only thing I changed was to comment out open() call and uncomment sysopen(). UID stays the same. Also, I made sure the directory had the right permissions.