All this logic should use the Process component instead of using proc_open manually (the Process component contains a huge amount of work to make it work consistently)