Why do you do this? That is a very resource intensive thing to be doing so often... And the post counts are reset to zero because the last transaction is to update the post count field.
This is a web server error, just like most of your other PHP issues, generally 500 errors are going to be related to your web server and its configuration, and several are very easy to just Google the error message
Google Search - The gateway did not...from the upstream server or application. and you'll find the Stack Overflow post
Gateway Time-out:The gateway did not receive a timely response from the upstream server.
This directly being a web server configuration issue with "your max_execution_time" and possibly "default_socket_timeout" (if configured in your php.ini). Again, UBB.threads is a basic web script that depends on PHP to be configured to be compatible with standard web scripts, which should be set by your web host. These requests are beyond the level of support offered here, which is support for the UBB.threads title directly and not misconfiguration web hosting environments.
You need to sync your settings that work for your forums to ALL php.ini files installed on the server (each version available to you has its own configuration file) so that when you change versions things work as expected.