A square and a rectangle have the same perimeters. The length of a side of the square is 4x-1. The length of the rectangle is 2x+1 and the width is x+2. Write and solve an equation to find x.