In MOST cases, you clear immigration or customs at your first port of entry of the country you're visiting. If you're connecting thru and transiting one country to another country (your final destination), it is up to the transiting country to set their own procedures.