If a party brought before the Court or Judge on the return of the writ is not entitled to his discharge, and is not bailed, where such bail is allowable, the Court or Judge must remand him to custody or place him under the restraint from which he was taken, if the person under whose custody or restraint he was is legally entitled thereto.