Nick Mathewson wrote:
At what stage would one be able to contact an attorney?
Great question, and the answer illustrates a central idea to all this.
You would contact an attorney after it is all over. All of your remedies in this situation are retrospective.
You can have a friend keep track of you in case you disappear for a few hours. You can also have an attorney standing by to file a federal habeas application in the nearest federal district court, but immigration officials will usually release you before the federal court has an opportunity to act.
Your remedies afterward include what's called a "Bivens" claim, named after the federal case creating a cause of action against federal officials for civil rights violations. The relief available is limited and presents great procedural obstacles. Still, anyone stopped and held for hours and searched should talk to a lawyer afterward.