The party imprisoned or restrained may deny allegations of fact in the return or statement and may allege other material facts. The court or justice may, in a summary way, examine the cause of imprisonment or restraint, hear evidence produced on either side, and if no legal cause is shown for such imprisonment or restraint, the court or justice shall discharge him, except as provided in section 5516.
14 M.R.S. § 5523