Acts 2:38 38 Peter replied, “Repent and be baptized, every one of you, in the name of Jesus Christ for the forgiveness of your sins. And you will receive the gift of the Holy Spirit.
Here we see that when a person repents and be baptized, the person will receive the Holy Spirit. When a person receives the Holy Spirit, he or she is born again. This is general case.
There is a special case when Peter preached the gospel the first time to the Gentile (Acts 10), to the Cornelius household. People received the Holy Spirit first before baptism, when the Gentile received the Holy Spirit, people realized that Gospel is not just for the Jews, but also for the Gentiles, then they baptized them.
Another special case is in Acts 19:2, Paul asked them “Did you receive the Holy Spirit when you believed”, they said no. They only received John’s baptism of repentance, they didn’t receive the Holy Spirit, later they were baptized in the name of Jesus to receive the Holy Spirit. This is the case of receiving the Holy Spirit after the baptism.
So in general a person receives the Holy Spirit at baptism, but there are special cases.