please can someone tell me if the order number is allocated after the customer is returned from the payment site or before being sent to the payment site?

i'm writing a new payment module, but just cannot find when it's allocated.
In my case, it's needed before, i.e in the hidden fields of the final process button string.