Welcome to the forum :wave:
Pops and bangs must be the 320 horses trying to get out
The pipe to the ECU is for vacuum/boost sensing, to adjust the ignition timing. It's pretty important - if not connected, the ignition timing could be quite wrong (retarded). The small pipe should run to a T-junction with the pipe going to the dump valve/blow-off valve (as applicable). And yes, they both connect to the inlet manifold (or more correctly, plenum chamber).
If the calamities happen at 4000RPM, is there also a good deal of boost at that stage? Does the amount of boost make any difference? I would expect that more boost = more problems?
If so, it sounds like it is too lean, so perhaps the airflow meter is faulty (not registering the full airflow) or perhaps you have an inwards air leak between the airflow meter and the turbocharger. Check the large pipe carefully for splits and make sure the clamps are tightened in the right places.
Have you checked/replaced spark plugs and leads?
Some details of the modifications to get 320bhp would be good, too
Cheers,
-Alex